How to make script remember a counter and display an ad every 5th time?
I am finishing up a project for my university and I need to add a monetization method. I stopped at Interstitials from AdMob that will be displayed whenever the GameOver screen appears. My problem is that I want them to appear on every fifth appearance of this GameOver screen and so far I haven't managed to make anything work. The interstitial appears ever time. Another problem that may affect this is that the script restarts every time I press Restart, so the ad counter won't really work. Here is my code and I hope someone can assist!
Thank you!
 public class GameOver: MonoBehaviour {
 
     static int loadCount = 0;
 
     private InterstitialAd interstitial;
     public GameObject gameOverScreen;
 
 
     bool gameOver;
 
     // Use this for initialization
     void Start () {
         FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;
         this.RequestInterstitial();
 
     }
 
     // Update is called once per frame
     public void Update () 
         if (gameOver)
         {
             if (Input.GetKeyDown (KeyCode.Space))
             {
                 SceneManager.LoadScene(1);
             }
         }
     }
 
     private void RequestInterstitial()
 
     {
 #if UNITY_ANDROID
         string adUnitId = "ca-app-pub-3940256099942544/1033173712";
 #elif UNITY_IPHONE
         string adUnitId = "ca-app-pub-3940256099942544/4411468910";
 #else
         string adUnitId = "unexpected_platform";
 #endif
 
         // Initialize an InterstitialAd.
         interstitial = new InterstitialAd(adUnitId);
 
         AdRequest request = new AdRequest.Builder().Build();
         // Load the interstitial with the request.
         interstitial.LoadAd(request);
     }
 
     private void AdDisplay()
     {
         if (interstitial.IsLoaded())
         {
             interstitial.Show();
         }
     }
 
     void OnGameOver()
     {
 
         gameOverScreen.SetActive (true);
         kmHighscore.text = GetScore().ToString("F0");
 
         gameOver = true;
         if (loadCount % 5 == 0) 
         {
             AdDisplay();
         }
         loadCount++;
     }
 }
 
              Answer by vanjasretenovic123 · Feb 05, 2018 at 08:10 PM
Try to save float with
PlayerPrefs.SetFloat("name of variable", value of variable); Eg. ("loadCount",5); or ("loadCount", loadCount) ; PlayerPrefs.Save();
And get float with
float newcount= PlayerPrefs.GetFloat("loadCount",newcount);
And then compare value of newcount
if(newcount == 5) { //loadAD }
I need your help too. I made script for show rewarded video admob, but ads are loaded, but not showing.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Google$$anonymous$$obileAds; using Google$$anonymous$$obileAds.Api;
public class GoogleAd$$anonymous$$obADS : $$anonymous$$onoBehaviour { public bool pr; // Use this for initialization private RewardBasedVideoAd rewardBasedVideo;
 public void Start()
 {
     pr = false;
     // Get singleton reward based video ad reference.
     //this.rewardBasedVideo = RewardBasedVideoAd.Instance;
     // Get singleton reward based video ad reference.
     this.rewardBasedVideo = RewardBasedVideoAd.Instance;
     
     this.RequestRewardedVideo();
     
 }
 // Update is called once per frame
 void Update () {
     if (rewardBasedVideo.IsLoaded())
     {
         pr=true;
         rewardBasedVideo.Show();
     }
 }
 private void RequestRewardedVideo()
 {
     //ca-app-pub-3940256099942544/5224354917
     string adUnitId = "ca-app-pub-3940256099942544/5224354917";
     // Create an empty ad request.
     AdRequest request = new AdRequest.Builder().Build();
     // Load the rewarded video ad with the request.
     rewardBasedVideo.LoadAd(request, adUnitId);
 }
 
                  }
Or you can random show ads
float a=Random.Range(0,5); if(a==3) { //show AD }
hi, thanks a lot for this.
i am trying to show a adcall every 3 times randomly so that the ad does not get called every time when the game level starts.
is this code you posted a way to implement that?
float a=Random.Range(0,5); if(a==3) { //show AD }
i would try to post my ad call instead of //show AD
Your answer