Why does my game crash on my mobile when I try to load the scene with this interstitial ad script?
The game however does not crash in unity when I press run. I added this script to the Main camera. Void GameOver() runs when the game over screen fades in. This is the script:
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class interstitialAds : MonoBehaviour
{
private InterstitialAd interstitial;
private bool shown;
private int count;
void Start()
{
count = PlayerPrefs.GetInt("Count");
RequestInterstitial();
if (count > 5)
{
GameOver();
count = 0;
PlayerPrefs.SetInt("Count", count);
}
count += 1;
PlayerPrefs.SetInt("Count", count);
}
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd(request);
}
void Update()
{
if (shown == false)
{
GameOver();
Debug.Log("Not yet...");
}
}
public void GameOver()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
shown = true;
}
else
{
Debug.Log("Interstitial is not ready yet.");
}
}
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}
Comment