Reputation: 59
In my current Unity program I wanted to implement ads. The ads do work in the Unity editor when I run the game, but when I try to run the ads on my iPhone 7 or iPad Air, there are no ads showing up. Does someone know what I am doing wrong?
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Advertisements;
public class GameManager : MonoBehaviour
{
void Start()
{
Advertisement.Initialize("Appstore-id");
}
bool gameHasEnded = false;
public float restartDelay = 1f;
public float addDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel ()
{
completeLevelUI.SetActive(true);
Invoke("ShowAdd", addDelay);
}
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("Game over");
Invoke("Restart", restartDelay);
}
}
public void ShowAdd()
{
if (Advertisement.IsReady())
{
Advertisement.Show ();
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Upvotes: 3
Views: 3360
Reputation: 687
I had the same problem and the way I fixed it was a recursive function until the ad was ready. Something like:
IEnumerator ShowAd()
{
yield return new waitForSeconds(1);
if (Advertisement.IsReady())
{
Advertisement.Show ();
}
else { StartCoroutine(ShowAd()); }
}
This way it will call the method until the ad is ready to show. Also, make sure you have test (or debug) mode enabled on Unity Ads Settings.
Upvotes: 2
Reputation: 1566
Just to add another option besides recursively starting an IEnumerator:
IEnumerator ShowAd()
{
while(!Advertisement.isReady())
{
yield return new waitForSeconds(1);
}
Advertisement.Show();
}
Upvotes: 0