I am almost finished developing my first game and how else am I going to make money if not through google admob.
Anyways, I made an empty game object for the Interstitial ads (the ones that cover the whole screen but can be skipped) and one for the rewarded ones. I made two scripts, one for the Interstitial ads and one for the rewarded ones, obviously. I pretty much copied the code given by google here. These are my two scripts, which are both attributed directly to their corresponding game object.
InterstitialScript.cs
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class InterstitialScript : MonoBehaviour
{
private InterstitialAd interstitial;
public static InterstitialScript instance;
void Awake()
{
instance = this;
}
void Start()
{
//InterstitialScript = this;
MobileAds.Initialize(initStatus => { });
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
public void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-9471668556390079~8501400148";
#elif UNITY_IPHONE
string adUnitId = null;
#else
string adUnitId = "unexpected_platform";
#endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
void Update()
{
//when user clicks continue, show interstitial
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
RewardedScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class RewardedScript : MonoBehaviour
{
private RewardedAd rewardedAd;
public static RewardedScript instance;
void Awake()
{
instance = this;
}
public void Start()
{
MobileAds.Initialize(initStatus => { });
RequestRewarded();
}
public void RequestRewarded()
{
string adUnitId;
#if UNITY_ANDROID
adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
adUnitId = "unexpected_platform";
#endif
this.rewardedAd = new RewardedAd(adUnitId);
// Called when an ad request has successfully loaded.
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// Called when an ad request failed to load.
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// Called when an ad request failed to show.
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Called when the ad is closed.
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);
}
public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdLoaded event received");
}
public void HandleRewardedAdOpening(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdOpening event received");
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToShow event received with message: "
+ args.Message);
}
public void HandleRewardedAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdClosed event received");
}
public void HandleUserEarnedReward(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardedAdRewarded event received for "
+ amount.ToString() + " " + type);
}
public void ShowRewarded()
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
}
}
I request the ads with either
InterstitialScript.instance.RequestInterstitial
or
RewardedScript.instance.RequestRewarded
The first one (InterstitialScript.cs) works just fine, however, the one for the rewarded ads doesn't work at all!
Im getting this error (once per frame I think):
NullReferenceException: Object reference not set to an instance of an object
InterstitialScript.Update () (at Assets/Scripts/InterstitialScript.cs:48)
The rest of the game works just fine but the error pops up and the ad doesnt show up.