- Home /
How to make your script function every 5 times it is called.
Hello. I have it so that when you press restart an advertisement is called. Each time you press restart is to restart the game (it's a high score based one). I want it so that my advertisement is shown every 5 games so you play it first time an ad shows, 5 games later another one shows . Please Help. I have an ads script and restart script if necessary.
ads script
 public class admob_ads : MonoBehaviour
 { 
 
     private InterstitialAd interstitial;
     string appId;
     string adUnitId;
     bool shoed;
     public bool isTest = true;
     private void Awake()
     {
         shoed = false;
         if (isTest)
         {
             appId = "ca-app-pub-3940256099942544/103317312"; //Google admob test ID
         }
         else
         {
             appId = ""; // My app ID (not visible for security reasons)
         }
 
         MobileAds.Initialize(appId);
 
     }
     private void Start()
     {
         reqint();
     }
 
     private void reqint()
     {
         adUnitId = "";  //Interstitial ad ID
         interstitial = new InterstitialAd(adUnitId);
         AdRequest request = new AdRequest.Builder().Build();
         interstitial.LoadAd(request);
     }
 
     public void Retry() //The part which makes it when i click retry an advert is shown
     {
         if (!shoed)
         {
             shoed = true;
             Debug.Log("Ads Show");
             if (interstitial.IsLoaded())
             {
                 interstitial.Show();
             }
         }
     }
 }
restart If (necessary)
 using UnityEngine;
      using UnityEngine.SceneManagement;
      using System.Collections;
 
 public class Restart : MonoBehaviour {
 
     public void RestartGame() {
         SoundManager.PlaySound("gameover");
         SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene
          }
 }
 
 
 
Answer by rounix_unity · Oct 29, 2019 at 05:41 PM
This is how i would do that:
 if(restarted)
 restart_count++;
 
 if(restart_count == 5)
    show_ad();
    restart_count = 0;
@rounix_unity I dont think the system would understand what restarted is and where would i slot that in my code?
Insted of 'restarted' just use a part of your code that is used for restarting and ins$$anonymous$$d of show_ad() use part of your code that is responsible for showing ads
So 'the ad shown bit is this:
 public void Retry() //The part which makes it when i click retry an advert is shown
      {
          if (!shoed)
          {
              shoed = true;
              Debug.Log("Ads Show");
              if (interstitial.IsLoaded())
              {
                  interstitial.Show();
and the restart bit is
 public void RestartGame() {
          Sound$$anonymous$$anager.PlaySound("gameover");
          Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); // loads current scene
how would i implement it (a bit confused)
Your answer
 
 
             Follow this Question
Related Questions
Admob ads not showing when using Unity 2018 0 Answers
Renderer on object disabled after level reload 1 Answer
Admob ads don't work 0 Answers
Using AdMob with the Designed for Families programme 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                