- Home /
Unity Ads doesn't Initialize properly
Hello, I recently published a game to the google play store, with unity ads. The ads work for most people but on my test device when I click to show the ad it doesn't appear. I have narrowed it down to a problem with Initialization, since tried running a loop that loops while (!Advertisemet.IsInitialzed){ Advertisement.Initialize("code", false) }
also when I initialize with test units it works fine, so it has something to do with the internet connection I am guessing. And yet I am receiving emails and I can surf the web so it shouldn't be a problem with my device. Also I have initialized the ads as soon as the game opens. So I don't know what it wrong because it only happens on a limited number of devices. please help me.
This is the code that the button uses:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class WatchUnityAd : MonoBehaviour
{
public int numOfCoinsToAdd = 5;
bool paused = false,CRRuning = false;
// Use this for initialization
void Start()
{
//Advertisement.Initialize("66223",false);
}
public void ShowAd(string zone = "")
{
#if UNITY_EDITOR
StartCoroutine(WaitForAd());
#endif
if (string.Equals(zone, ""))
zone = null;
/*while (!Advertisement.isInitialized)
{
Advertisement.Initialize("66223", false);
}*/
ShowOptions options = new ShowOptions();
options.resultCallback = AdCallbackhandler;
if (Advertisement.IsReady())
Advertisement.Show(zone, options);
}
void AdCallbackhandler(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("Ad Finished. Rewarding player...");
int coinTotal = PlayerPrefs.GetInt("coinTotal");
coinTotal += numOfCoinsToAdd;
PlayerPrefs.SetInt("coinTotal", coinTotal);
gameObject.SetActive(false);
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;
}
}
PS. I implemented exactly the code that appears in the live session about unity ads, and all I did was that ShowAd() is run when a button is pressed. PPS. if you need me to give anymore information so you could help me let me know because this is an important fix for me because the game is already published.
Thank you!