🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Null reference exception while calling methods from Admanager Script.

Started by
2 comments, last by Alberth 5 years, 3 months ago

I have done everything but cant sort out that problem. The problem is that when I call the Requestbanner(); and showfullscreenad(); It gives me an error called null reference exception I am getting that error form days and cant sort it out can any one please help me out. I will be really thank for. Peace :)

Code of Ad manager: 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using GoogleMobileAds.Api;
using System;


public class AddManagers : MonoBehaviour
{
public static AddManagers instance;

public string AppId;

private BannerView bannerView;

public string bannerId;

private InterstitialAd FulScreenAdd;

public string FullScreenId;

private void Awake()
{
if (instance = null)
{
instance = this;
}
else
{
Destroy(this);
}
}

private void Start()
{
MobileAds.Initialize(AppId);

RequestFullScreenAdds();

FulScreenAdd.OnAdClosed += HandlerOnCloased;
}

public void RequestBanner()
{
bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.Top);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
bannerView.Show();
}

public void HideBanner()
{
bannerView.Hide();
}

public void RequestFullScreenAdds()
{
FulScreenAdd = new InterstitialAd(FullScreenId);
AdRequest request = new AdRequest.Builder().Build();
FulScreenAdd.LoadAd(request);

}

public void HandlerOnCloased(object sender, EventArgs args)
{

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

public void ShowFullScreenAdd()
{
if(FulScreenAdd.IsLoaded())
{
FulScreenAdd.Show();
}
else
{
Debug.Log("Adds are not loaded");
}


}
}
 

Advertisement

Could you post the text of the error message, and indicate which line or lines cause it?

A standard pattern in code if you ask for something is that it either gives you the thing you ask for or it returns a null indicating that your request failed.

You daisy-chain requests, like "SceneManager.GetActiveScene().buildIndex" by just appending ".NextName..."-like calls. Here you start with SceneManager, where you query ".GetActiveScene()". From the result of that call you query ".buildindex".

In the general case, SceneManager may not exist (read: be null), and you cannot query anything from the scene manager in that case. If scene manager does exist, you can perform "SceneManager.GetActiveScene()", but there may not be an active scene, so this would return null, and then you cannot ask the ".buildindex" from it.

tldr: After each step in the daisy-chain, check if the result of the call is not null, and only after verifying, perform the next step in the daisy-chain. The only exception to this rule is if the documentation of a call states that the call will never fail.

The above line would become something like
 


// Completely made up the return types, but I hope the general idea is more clear.
// First check that it is safe to jump before you try.

if (SceneManager == null) {
  // no scene manager! panic
} else {
  Scene scene = SceneManager.GetActiveScene();
  if scene == null) {
    // no scene! panic
  } else {
    int index = scene.buildIndex;
    // do something with the index.
  }
}

 

This topic is closed to new replies.

Advertisement