NullReference error in Unity when trying to use Banner.Destroy();

812 views
Skip to first unread message

casual...@gmail.com

unread,
Nov 27, 2016, 11:32:42 PM11/27/16
to Google Mobile Ads SDK Developers
Hey guys,

I have been trying to use Admob in my unity game ( and I got it to work) , however , it started to show on all my scenes. So I decided to use BannerView.Destroy() to make it disappear in some places , and appear in others. However , I keep getting this error when I try to use it in my script.

NullReferenceException: Object reference not set to an instance of an object
TestLevel.Start () (at Assets/Scripts/TestLevel.cs:16)


Here is the script

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class TestLevel : MonoBehaviour
{
    public Request req;
    public BannerView bannerView;

    // Use this for initialization
    void Start()
    {

        if (Application.loadedLevel == 1)
        {//error here

            req.bannerView.Destroy();// error occurs here
            Debug.Log(" Banner Destroyed");
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

}

casual...@gmail.com

unread,
Nov 27, 2016, 11:36:10 PM11/27/16
to Google Mobile Ads SDK Developers


I'm Using Unity 5.4, and 3.12 of Google Ads Unity plugin. I am using the latest Google play SDK plugins.

Joshua Lagonera (Mobile Ads SDK Team)

unread,
Nov 28, 2016, 6:56:15 AM11/28/16
to Google Mobile Ads SDK Developers
Hi there,

You are experiencing a Null Reference error because you are destroying a banner that may not be initialized yet.
I would recommend that you destroy the Banner View using bannerView.Destroy() in the OnDisable() method rather than in Start() so the banner will be destroyed when the scene becomes inactive or is disabled.

Regards,
Joshua Lagonera
Mobile Ads SDK Team

casual...@gmail.com

unread,
Nov 28, 2016, 10:42:18 PM11/28/16
to Google Mobile Ads SDK Developers

I tried , and it still gives me the error
NullReferenceException: Object reference not set to an instance of an object
TestLevel.OnEnable () (at Assets/Scripts/TestLevel.cs:26)

This script below is attached to my first scene ( where my banner loads)
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
    public BannerView bannerView;
    public void Start()
    {

#if UNITY_EDITOR
        string adUnitId = "hh";
#elif UNITY_ANDROID
        string adUnitId = "hh";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();

        bannerView.LoadAd(request);

    }

    // Update is called once per frame
    void Update()
    {

    }
}

This script below is attached to my second scene ( where I want my banner to be destroyed). This is also why I used OnEnable rather than on Disable ( I got the error for both anyway). This script references the banner created in the first scene , and destroys it if it's my gameplay level . This script is where the error occurs.

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class TestLevel : MonoBehaviour
{
    public Request req;
    public BannerView bannerView;

    // Use this for initialization
    void Start()
    {


    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnEnable() {
        if (Application.loadedLevel == 1)
        {

Joshua Lagonera (Mobile Ads SDK Team)

unread,
Nov 29, 2016, 1:30:01 AM11/29/16
to Google Mobile Ads SDK Developers
Hi there,

As previously stated, you are experiencing the NullReference Error because you are trying to access and destroy an object that may or may have not been initialized. 
That said, I noticed a few things about your code:
  • You created a public bannerView on your Request script, but you are creating a local variable of the same name in the Start() method. This means you are not assigning any value to your public bannerView.
  • You are not assigning any value to the Request object "req" in your Test Level script, which explains why you are receiving the NullReference Error.
Now I would have two recommendations for you to resolve the issue:
  • Destroy your public bannerView inside your Request script on its onDestroy or onDisable method before moving onto the next scene; or
  • Create a local instance variable for each scene that you want a BannerView to appear.
Additionally, I would like to include this thread on how to access variables from different scenes. 

Regards,
Joshua Lagonera
Mobile Ads SDK Team

casual...@gmail.com

unread,
Dec 1, 2016, 8:31:16 PM12/1/16
to Google Mobile Ads SDK Developers
Thanks man. This seemed like it was going to work , but I booted up Unity and randomly got this error in the editor (EVERY FRAME)
NullReferenceException: Object reference not set to an instance of an object
GooglePlayServices.PlayServicesResolver.AutoResolve ()
UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorApplicationBindings.gen.cs:197)

It still allows me to build , but then this error occurs
Moving
Temp/StagingArea/android-libraries/play-services -ads -9.80/classes.jar
to
Temp/StagingArea/android-libraries/play-services -ads -9.80/libs/classes.jar:
The system cannot find the file specified.

Keep in mind , I have updated to the latest SDK settings and I have the latest Unity. What's going on?

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Dec 2, 2016, 11:23:12 AM12/2/16
to Google Mobile Ads SDK Developers
Hi there,

It looks like an issue with your Android SDK. Can you make sure that Unity is pointing to the correct Android SDK path from its Preferences and that you have Google Play Services, Android SDK Build Tools and Support Repository frameworks updated from the Android SDK Manager? I'd suggest to clean build the Unity IDE and check that there aren't any duplicate Android SDK folders.

Thanks,
Arjun Busani
Mobile Ads SDK Team

casual...@gmail.com

unread,
Dec 2, 2016, 12:09:34 PM12/2/16
to Google Mobile Ads SDK Developers
Forgive me , but how would I clean build? I've checked for duplicate files , and the path and everything is right. The SDK Manager has also updated everything.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Dec 2, 2016, 4:53:20 PM12/2/16
to Google Mobile Ads SDK Developers
Hi there,

Then try to create a new project and only include our Mobile Ads plugin and see if you are able to build. 

Thanks,
Arjun Busani
Mobile Ads SDK Team

casual...@gmail.com

unread,
Dec 3, 2016, 4:39:04 PM12/3/16
to Google Mobile Ads SDK Developers
I made a new project and it builds. So what's wrong with my other project?

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Dec 5, 2016, 10:28:41 AM12/5/16
to Google Mobile Ads SDK Developers
Hi there,

Glad that it worked. I would not be able to specify what you might be missing in your original project since I do not have the build but I would suggest you to recheck for any script referencing issues when you make that ad request. 

Thanks,
Arjun Busani
Mobile Ads SDK Team

Reply all
Reply to author
Forward
0 new messages