- Home /
Unity ads strange problem
Hello forum, I've been searching all night for someone who's encountered the same issue as me here but I haven't found anybody so I'll post my problem.
Short version - ads show the first time when going from my menu to my level, but when I go back to my main menu and try to start the next level, the advertisement displays and the close button in the top right corner of the ad doesn't do anything.
Now in case there is some specific crucial problem I'm creating here I'll explain more about the whole process of moving between levels and calling ShowAd(). - When my game starts I call Advertisement.Initialize() in awake() and when the player starts a level I call ShowAd() using some code which I ripped right out of the unity tutorial for unity ads. The demo ad shows fine and when I click close, my game proceeds to the level as expected. The showAd() and Advertisement.Initialize() functions are called from a singleton object that I use to load the player profile, write profile data to the menu, and load scenes.
Upon completing the level, I save the gamer profile and destroy the singleton where I called the ShowAd() function from before returning to the main menu. (This is so that when I return to my main menu I can get all the references to all the menu items and images etc that I have assigned through the editor, without having to find them through script in the start function of the singleton (I have it set to not destroy on load so that it is present in the level but I destroy it before returning to the menu so I can get a fresh one)).
So when the main menu loads again there is a fresh singleton object there and the game runs the load process of the player profile again. Everything is fine in my menu scene and all data is loaded just as it was before the level was played. However when I try to play the level again and showAd() is called for the second time in the game session the button to close the ad has no effect at all. (Different object calling it now, same class but different instance, as the first one was destroyed at the end of the level).
I included the process here because I'm not sure if it matters... One thing I thought was maybe Advertisement.Initialize() could only be called once per session but I read online that it doesn't matter. If anything is unclear let me know and I can provide some more details.
One final note, there's no error or message in the editor console or in the more in depth Editor.txt log. Just "UnityAdsEditor: Show(, );"
public void ShowAd(string zone = "")
{
#if UNITY_EDITOR
StartCoroutine(WaitForAd ());
#endif
if (string.Equals (zone, ""))
zone = null;
ShowOptions options = new ShowOptions ();
options.resultCallback = AdCallbackhandler;
if (Advertisement.IsReady (zone))
Advertisement.Show (zone, options);
}
void AdCallbackhandler (ShowResult result)
{
switch(result)
{
case ShowResult.Finished:
Debug.Log ("Ad Finished. Rewarding player...");
break;
case ShowResult.Skipped:
Debug.Log ("Ad skipped. Son, I am dissapointed in you");
break;
case ShowResult.Failed:
Debug.Log("I swear this has never happened to me before");
break;
}
}
IEnumerator WaitForAd()
{
float currentTimeScale = Time.timeScale;
Time.timeScale = 0f;
yield return null;
while (Advertisement.isShowing)
yield return null;
Time.timeScale = currentTimeScale;
}