Search Unity

Ads not always working

Discussion in 'Unity Ads & User Acquisition' started by pit84, Jul 4, 2017.

  1. pit84

    pit84

    Joined:
    Jul 4, 2017
    Posts:
    1
    I have a button with assigned code:

    Code (CSharp):
    1.     public void QuitGame()
    2.     {
    3.         ShowAd();
    4.         Application.Quit();
    5.     }
    6.  
    7.     public void ShowAd()
    8.     {
    9.         Advertisement.Show();
    10.     }
    11.  
    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:
    Code (CSharp):
    1. public void QuitGame()
    2.     {
    3.         ShowAd();
    4.     }
    5.  
    6. public void ShowAd()
    7.     {
    8.         if (Advertisement.IsReady())
    9.         {
    10.             Advertisement.Show("video", new ShowOptions() {resultCallback = HandleAdResult});
    11.         }
    12.     }
    13.  
    14.     private void HandleAdResult(ShowResult result)
    15.     {
    16.         switch (result)
    17.         {
    18.             case ShowResult.Finished:
    19.                 Application.Quit();
    20.                 break;
    21.             case ShowResult.Skipped:
    22.                 Application.Quit();
    23.                 break;
    24.             case ShowResult.Failed:
    25.                 Application.Quit();
    26.                 break;
    27.         }
    28.     }
    29.  
    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
     
  2. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    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:

    Code (csharp):
    1.  
    2. public GameObject AdButton;
    3.  
    4. IEnumerator waitForAdReady() {
    5.   AdButton.SetActive(false); //
    6.   while (!Advertisement.IsReady())
    7.     yield return null;
    8.   AdButton.SetActive(true);
    9. }
    10.  
    Then the AdButton would run your function for showing an ad.

    I hope this helps!
     
  3. grapebutt_real

    grapebutt_real

    Joined:
    Jul 20, 2017
    Posts:
    1