Ads not always working

I have a button with assigned code:

    public void QuitGame()
    {
        ShowAd();
        Application.Quit();
    }

    public void ShowAd()
    {
        Advertisement.Show();
    }

That’s the simple version. In editor it works fine, pressing button shows screen that says it is an add screen.
After building apk, pressing the button simply causes game to exit, without playing any advert.

Other code:

public void QuitGame()
    {
        ShowAd();
    }

public void ShowAd()
    {
        if (Advertisement.IsReady())
        {
            Advertisement.Show("video", new ShowOptions() {resultCallback = HandleAdResult});
        }
    }

    private void HandleAdResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                Application.Quit();
                break;
            case ShowResult.Skipped:
                Application.Quit();
                break;
            case ShowResult.Failed:
                Application.Quit();
                break;
        }
    }

For simplicity, I did not add any extra code for different Results, just Application.Quit(). After building this, the ad sometimes plays, sometimes does not. If it plays, everything works fine, game quits after. If it does not, pressing button does not have any effect.

I am very beginner to ad system and can’t find what is wrong, would appreciate any help

Hi,

The code below should work otherwise, except for the fact that Advertisement.IsReady() returns false while the ad is being downloaded. You may want to consider having a loop where you check whether the ads are ready and enabling the button to show ads when IsReady() returns true.

Example:

public GameObject AdButton;

IEnumerator waitForAdReady() {
  AdButton.SetActive(false); // 
  while (!Advertisement.IsReady())
    yield return null;
  AdButton.SetActive(true);
}

Then the AdButton would run your function for showing an ad.

I hope this helps!