-3

I have been trying to make my game, play an ad every 5 rounds/losses. I have tried copying some scripts, but they don't work for me. I am a really early developer and don't know much about c# and unity. If you find a solution tell me in which script I need to put the code. My GameManager script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject gameOverCanvas;
    public AdsManager ads;

    private void Start()
    {
        Time.timeScale = 1;
        ads.ShowBanner();
    }

    public void GameOver()
    {
        gameOverCanvas.SetActive(true);
        Time.timeScale = 0;
        ads.PlayAd();
    }

    public void Replay()
    {
        SceneManager.LoadScene(0);
    }

}

My AdsManager script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour, IUnityAdsListener
{
#if UNITY_IOS
    string gameId = "#######";
#else
    string gameId = "#######";
#endif

    Action onRewardedAdSuccess;

    // Start is called before the first frame update
    void Start()
    {
        Advertisement.Initialize(gameId);
        Advertisement.AddListener(this);
        ShowBanner();
    }

    public void PlayAd()
    {
        if(Advertisement.IsReady("Interstitial_Android"))
            Advertisement.Show("Interstitial_Android");
    }

    public void PlayRewardedAd(Action onSuccess)
    {
        onRewardedAdSuccess = onSuccess;
        if(Advertisement.IsReady("Rewarded_Android"))
        {
        Advertisement.Show("Rewarded_Android");
        }
        else
        {
            Debug.Log("Rewarded ad is not ready!");
        }
    }

    public void ShowBanner()
    {
        if (Advertisement.IsReady("Banner_Android"))
        {
            Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
            Advertisement.Banner.Show("Banner_Android");
        }
        else
        {
            StartCoroutine(RepeatShowBanner());
        }
    }

    public void HideBanner()
    {
        Advertisement.Banner.Hide();
    }

    IEnumerator RepeatShowBanner()
    {
        yield return new WaitForSeconds(1);
        ShowBanner();
    }

    public void OnUnityAdsReady(string placementId)
    {
        Debug.Log("ADS ARE READY!");
    }

    public void OnUnityAdsDidError(string message)
    {
        Debug.Log("ERROR: " + message);
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        Debug.Log("VIDEO STARTED!");
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
        {
            onRewardedAdSuccess.Invoke();
        }
    }
}

If you need any more scripts then tell me. I would appreciate any feedback.

1 Answers1

0

Create a counter, increment it after every game and then check if you reached required number of games.

You would need to create counter and required games variables. Then, put the increment and if statement code in your GameOver method:

public void GameOver()
{
    gameOverCanvas.SetActive(true);
    Time.timeScale = 0;
    
    gameCount++; //increment game count

    if(gameCount >= gamesToShowAd) // check if player played enough games to show ad
    {
        ads.PlayAd();
        gameCount = 0; // reset counter
    }
}

If you wish, you could consider saving game count using https://docs.unity3d.com/ScriptReference/PlayerPrefs.html or using ready online solution, but it's up to you.

bartol44
  • 562
  • 2
  • 12
  • Thanks for the answer! I'm an early developer and it would be nice of you to tell me what kind of variables I need to add to my code and how to create a counter. I almost have no idea what I'm doing. Sorry if I'm too ask full! – Niks Rodžers Aug 31 '22 at 16:40
  • gamCount is your counter - as you can see this variable is being incremented and checked. You need to declare those two variables I used: int gameCount = 0; int gamesToShowAd = 5; – bartol44 Aug 31 '22 at 16:52
  • Just be sure to declare them inside the class, not method - otherwise they will reset themselves everytime you call it. – bartol44 Aug 31 '22 at 16:54
  • How do you do that? – Niks Rodžers Aug 31 '22 at 17:08
  • How do you declare a variable? If you went so far you should know from this description. I suggest you do some basic c# tutorial, and I say it in good faith, not to mock you, because variables is the first concept you learn. – bartol44 Aug 31 '22 at 18:00
  • Okay. I know simple c# like variables classes, methods, functions, etc. I copied the code you showed and declared the variables. And added the variables ' int gameCount = 0; int gamesToShowAd = 5;' The code works when I type 1 in the gamesToShowAd. And as expected it shows an ad every round. Why does it just show it every round, but not what I put in the gamesToShowAd variable? Or maybe I'm just stupid (probably). – Niks Rodžers Aug 31 '22 at 18:21
  • You mean that it doesn't matter if you put 1 or 5 in the gamesToShowAd? Where did you declare your variables. I would need your updated code. – bartol44 Aug 31 '22 at 21:49
  • This is my updated code: https://www.linkpicture.com/view.php?img=LPic6310667b6a891268204364 – Niks Rodžers Sep 01 '22 at 08:03
  • This link doesn't work for me, browser is stuck on loading – bartol44 Sep 01 '22 at 08:13
  • Ok. Try this: https://ibb.co/fCQ4yzF – Niks Rodžers Sep 01 '22 at 08:20
  • I ran out of ideas. Try to experiment yourself: ran the debugger, check why you are entering code that starts an ad. Maybe it's if statement, maybe you call GameOver or watch and in other places. – bartol44 Sep 01 '22 at 08:38