Hi, I am Ayoub new to C# and unity at the start, plus I am doing this out of my interest, so my question could be basic. However it has bugged me for so long, that I couldn’t get it over even after I research it over the internet.
Please help me. thanks.
console: NullReferenceException: Object reference not set to an instance of an object AdsManager.ShowAdMob () (at Assets/Scripts/AdsManager.cs:104) AdsManager.ShowInterstitial () (at Assets/Scripts/AdsManager.cs:54) Home.OnNormalMode () (at Assets/Scripts/Home.cs:86) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165) UnityEngine.Events.UnityEvent1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:63) UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:67) UnityEngine.EventSystems.EventTrigger.OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:93) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerDownHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:36) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerDownHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction
1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
`using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GoogleMobileAds.Api;
using System;
using UnityEngine.Advertisements;
public class AdsManager : MonoBehaviour {
#region AdMob
[Header("Admob")]
public string adMobAppID;
public string interstitalAdMobId;
public string videoAdMobId;
InterstitialAd interstitialAdMob;
private RewardBasedVideoAd rewardBasedAdMobVideo;
AdRequest requestAdMobInterstitial, AdMobVideoRequest;
#endregion
[Space(15)]
#region
[Header("UnityAds")]
public string unityAdsGameId;
public string unityAdsVideoPlacementId = "rewardedVideo";
#endregion
public Text status;
static AdsManager instance;
public int interval;
private int counter;
private bool isDev = false;
public static AdsManager Instance
{
get
{
if(instance == null)
instance = GameObject.FindObjectOfType(typeof(AdsManager)) as AdsManager;
return instance;
}
}
void Awake ()
{
gameObject.name = this.GetType().Name;
DontDestroyOnLoad(gameObject);
InitializeAds();
}
public void ShowInterstitial()
{
if (isDev) return;
ShowAdMob();
}
public void IsVideoRewardAvailable()
{
}
public void ShowVideoReward()
{
if (isDev) return;
if (rewardBasedAdMobVideo.IsLoaded())
{
AdMobShowVideo();
}
else if (Advertisement.IsReady(unityAdsVideoPlacementId))
{
UnityAdsShowVideo();
}
}
private void RequestInterstitial()
{
// Initialize an InterstitialAd.
interstitialAdMob = new InterstitialAd(interstitalAdMobId);
// Called when an ad request has successfully loaded.
interstitialAdMob.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
interstitialAdMob.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is shown.
interstitialAdMob.OnAdOpening += HandleOnAdOpened;
// Called when the ad is closed.
interstitialAdMob.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
interstitialAdMob.OnAdLeavingApplication += HandleOnAdLeavingApplication;
// Create an empty ad request.
requestAdMobInterstitial = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitialAdMob.LoadAd(requestAdMobInterstitial);
}
public void ShowAdMob()
{
if (isDev) return;
if(interstitialAdMob.IsLoaded())
{
interstitialAdMob.Show();
}
else
{
interstitialAdMob.LoadAd(requestAdMobInterstitial);
counter++;
if(counter%interval==0)
UnityAdsShowVideo ();
}
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
status.text = args.ToString();
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
status.text = args.Message;
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
interstitialAdMob.LoadAd(requestAdMobInterstitial);
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeftApplication event received");
}
private void RequestRewardedVideo()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideo.OnAdLoaded += HandleRewardBasedVideoLoadedAdMob;
// Called when an ad request failed to load.
rewardBasedAdMobVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMob;
// Called when an ad is shown.
rewardBasedAdMobVideo.OnAdOpening += HandleRewardBasedVideoOpenedAdMob;
// Called when the ad starts to play.
rewardBasedAdMobVideo.OnAdStarted += HandleRewardBasedVideoStartedAdMob;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideo.OnAdRewarded += HandleRewardBasedVideoRewardedAdMob;
// Called when the ad is closed.
rewardBasedAdMobVideo.OnAdClosed += HandleRewardBasedVideoClosedAdMob;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMob;
// Create an empty ad request.
AdMobVideoRequest = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideo.LoadAd(AdMobVideoRequest, videoAdMobId);
}
public void HandleRewardBasedVideoLoadedAdMob(object sender, EventArgs args)
{
status.text = "Unity loaded";
}
public void HandleRewardBasedVideoFailedToLoadAdMob(object sender, AdFailedToLoadEventArgs args)
{
status.text = args.Message;
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMob(object sender, EventArgs args)
{
}
public void HandleRewardBasedVideoStartedAdMob(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMob(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideo.LoadAd(AdMobVideoRequest, videoAdMobId);
status.text = "unity ad rewareded";
}
public void HandleRewardBasedVideoRewardedAdMob(object sender, Reward args)
{
GameController.Instance.UnlockSong();
this.rewardBasedAdMobVideo.LoadAd(AdMobVideoRequest, videoAdMobId);
}
public void HandleRewardBasedVideoLeftApplicationAdMob(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
void InitializeAds()
{
MobileAds.Initialize(adMobAppID);
this.rewardBasedAdMobVideo = RewardBasedVideoAd.Instance;
this.RequestRewardedVideo();
Advertisement.Initialize(unityAdsGameId);
RequestInterstitial();
}
void AdMobShowVideo()
{
rewardBasedAdMobVideo.Show();
}
void UnityAdsShowVideo()
{
ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResultUnity;
Advertisement.Show(unityAdsVideoPlacementId, options);
}
void HandleShowResultUnity(ShowResult result)
{
if (result == ShowResult.Finished)
{
GameController.Instance.UnlockSong();
Advertisement.Initialize(unityAdsGameId);
}
else if (result == ShowResult.Skipped)
{
Debug.LogWarning("Video was skipped - Do NOT reward the player");
}
else if (result == ShowResult.Failed)
{
status.text = "unity failed";
}
}
bool isVideoAvaiable()
{
#if !UNITY_EDITOR
if(Advertisement.IsReady(unityAdsVideoPlacementId))
{
return true;
}
else if(rewardBasedAdMobVideo.IsLoaded())
{
return true;
}
#endif
return false;
}
}`