I am finishing up a project for my university and I need to add a monetization method. I stopped at Interstitials from AdMob that will be displayed whenever the GameOver screen appears. My problem is that I want them to appear on every fifth appearance of this GameOver screen and so far I haven’t managed to make anything work. The interstitial appears ever time. Another problem that may affect this is that the script restarts every time I press Restart, so the ad counter won’t really work. Here is my code and I hope someone can assist!
Thank you!
public class GameOver: MonoBehaviour {
static int loadCount = 0;
private InterstitialAd interstitial;
public GameObject gameOverScreen;
bool gameOver;
// Use this for initialization
void Start () {
FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;
this.RequestInterstitial();
}
// Update is called once per frame
public void Update ()
if (gameOver)
{
if (Input.GetKeyDown (KeyCode.Space))
{
SceneManager.LoadScene(1);
}
}
}
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
string adUnitId = "unexpected_platform";
#endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
private void AdDisplay()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
void OnGameOver()
{
gameOverScreen.SetActive (true);
kmHighscore.text = GetScore().ToString("F0");
gameOver = true;
if (loadCount % 5 == 0)
{
AdDisplay();
}
loadCount++;
}
}