Implementation period
Early April 2019
Operating environment
- Windows 10 64 Bit
- Unity 2018.3.9 64 Bit
- Google Admob Ads Unity Plugin Ver.3.16
- Script editor: Visual Studio 2017
Symptoms
Import Admob’s UnityPlugin and implement test ad
I tried to run a project but my ad is not showing.
Error message
NullReferenceException: Object reference is not set to instance of object
ADManager.HandleBannerADEvents (System.Boolean subscribe) (Assets / Scripts / ADManager.cs: 123)
Cause
It seems that a NULL exception error occurred because bannerAD failed to instantiate.
URL of tutorial for reference
YouTube:
Unity Game Monetization | Admob Unity Tutorial
Code
/* AdManager.cs */
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class ADManager: MonoBehaviour {
private string APP_ID = "";
private BannerView banner AD;
private InterstitialAd interstitialAd;
private RewardBasedVideoAd rewardVideoAd;
void Start () {
// this is when you publish your app
MobileAds.Initialize (APP_ID);
// RequestBanner ();
RequestInterstitial ();
RequestVideoAD ();
}
void RequestBanner () {
string banner_ID = "ca-app-pub-3940256099942544 / 6300978111"; // UnitID for test
if (bannerAD! = null) {
bannerAD.Destroy ();
}
bannerAD = new BannerView (banner_ID, AdSize.SmartBanner, AdPosition.Bottom);
// FOR REAL APP
// AdRequest adRequest = new AdRequest.Builder (). Build ();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder ()
.AddTestDevice ("2077ef9a63d2b398840261c8221a0c9b"). Build ();
bannerAD.LoadAd (adRequest);
}
void RequestInterstitial () {
string interstitial_ID = "ca-app-pub-3940256099942544 / 1033173712"; // UnitID for test
interstitialAd = new InterstitialAd (interstitial_ID);
// FOR REAL APP
// AdRequest adRequest = new AdRequest.Builder (). Build ();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder ()
.AddTestDevice ("2077ef9a63d2b398840261c8221a0c9b"). Build ();
interstitialAd.LoadAd (adRequest);
}
void RequestVideoAD () {
string video_ID = "ca-app-pub-3940256099942544 / 5224354917"; // UnitID for test
rewardVideoAd = RewardsBasedVideoAd.Instance;
// FOR REAL APP
// AdRequest adRequest = new AdRequest.Builder (). Build ();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder ()
.AddTestDevice ("2077ef9a63d2b398840261c8221a0c9b"). Build ();
rewardVideoAd.LoadAd (adRequest, video_ID);
}
public void Display_Banner () {
bannerAD.Show ();
}
public void Display_InterstitialAD () {
if (interstitialAd.IsLoaded ()) {
interstitialAd.Show ();
}
}
public void Display_Reward_Video () {
if (rewardVideoAd.IsLoaded ()) {
rewardVideoAd.Show ();
}
}
// HANDLE EVENTS
public void HandleOnAdLoaded (object sender, EventArgs args) {
// ad is loaded show it
Display_Banner ();
}
public void HandleOnAdFailedToLoad (object sender, AdFailedToLoadEventArgs args) {
// ad failed to load load it again
RequestBanner ();
}
public void HandleOnAdOpened (object sender, EventArgs args) {
MonoBehaviour.print ("HandleAdOpened event received");
}
public void HandleOnAdClosed (object sender, EventArgs args) {
MonoBehaviour.print ("HandleAdClosed event received");
}
public void HandleOnAdLeavingApplication (object sender, EventArgs args) {
MonoBehaviour.print ("HandleAdLeavingApplication event received");
}
void HandleBannerADEvents (bool subscribe) {
if (subscribe) {
// Called when an ad request has successfully loaded.
bannerAD.OnAdLoaded + = HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerAD.OnAdFailedToLoad + = HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerAD.OnAdOpening + = HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerAD.OnAdClosed + = HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerAD.OnAdLeavingApplication + = HandleOnAdLeavingApplication;
} else {
// Called when an ad request has successfully loaded.
bannerAD.OnAdLoaded-= HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerAD.OnAdFailedToLoad-= HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerAD.OnAdOpening-= HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerAD.OnAdClosed-= HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerAD.OnAdLeavingApplication -= HandleOnAdLeavingApplication;
}
}
void OnEnable() {
HandleBannerADEvents(true);
}
void OnDisable() {
HandleBannerADEvents(false);
}
} // class