show ad after 3 rounds does not work

hi guys,

I’m currently using admob with banner ads and that work’s flawlessly.
I wanted to create interstitial ads which trigger after the user played 3 rounds too but I can’t get it up and running.

finally got the ads to show but only if I do it everytime the player restarts and not with a counter of 3.

the code I’m using:

...

private static int counter = 0;
private static InterstitialAd interstitial;

  void Awake()
  {
    gameOver.SetActive(false);
    counter++;
    interstitial = new InterstitialAd("here is my ID");
    AdRequest request = new AdRequest.Builder().Build();
    interstitial.LoadAd(request);
  }



  public void playerDied()
  {
    if (gameOver != null)
      gameOver.SetActive(true);

    if (counter % 3 == 0)
    {
      if (interstitial.isLoaded()) 
      {
        interstitial.show();
      }
    counter = 0;
    }
    BackgroundParallax.stopping = true;
    Scrolling.stopping = true;
    ObjectSpawner.stopping = true;
  }
}

hope some is able to help. thanks !

All you need is an else condition to increment the counter if player died but ad is not shown since counter has not reached count of three.

Something like:

public void playerDied()
{
    if (gameOver != null)
       gameOver.SetActive(true);
 
    if (counter % 3 == 0)
    {
       if (interstitial.isLoaded()) 
       {
         interstitial.show();
       }
		counter = 0;
    } else
	{
		counter++; 
	}

    BackgroundParallax.stopping = true;
    Scrolling.stopping = true;
    ObjectSpawner.stopping = true;
}

You can even replace your

if (counter % 3 == 0)

to be:

if (counter >= 3)

to catch the situation where counter has gone above three but has not reached a value of something like 6. This is just in case, anything goes wrong.