Jump to content
  • Advertisement
BaqarAli

C# Null reference exception while calling methods from Admanager Script.

Recommended Posts

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");
}


}
}
 

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.
  }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!