Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object ?

Discussion in 'Scripting' started by Venatal, Jan 5, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    "NullReferenceException: Object reference not set to an instance of an object
    SoundFX.InCorrectCollision () (at Assets/Scripts/SoundFX.cs:23)
    CollisionScript.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/Scripts/CollisionScript.cs:16)"
    Code (CSharp):
    1.  
    2. public void InCorrectCollision()
    3.     {
    4.         PlaySound(1);
    5.         ScoreSystem.manager.GameOverScore();
    6.         Bannerad.manager.ShowBanner();
    7.     }
    8.  
    I'm getting the error for "Bannerad.manager.ShowBanner();"
    This is the BannerScript:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using GoogleMobileAds.Api;
    4.  
    5. public class Bannerad : MonoBehaviour
    6. {
    7.     public static Bannerad manager;
    8.     private BannerView bannerView;
    9.     bool Showing = false;
    10.  
    11.     void Awake()
    12.     {
    13.         RequestBanner();
    14.         manager = this;
    15.  
    16.         if (Showing == false)
    17.         {
    18.             bannerView.Show();
    19.             Showing = true;
    20.             return;
    21.         }
    22.     }
    23.  
    24.  
    25.     void RequestBanner()
    26.     {
    27. #if UNITY_ANDROID
    28.         string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx";
    29. #elif UNITY_IPHONE
    30.         string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    31. #else
    32.         string adUnitId = "unexpected_platform";
    33. #endif
    34.  
    35.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
    36.         AdRequest request = new AdRequest.Builder()
    37.         .AddTestDevice(AdRequest.TestDeviceSimulator)      
    38.         .AddTestDevice("xxxxxxxxxxxxxx")  
    39.         .Build();
    40.         bannerView.LoadAd(request);
    41.        
    42.     }
    43.  
    44.     public void ShowBanner()
    45.     {
    46.         bannerView.Show();
    47.         Showing = true;
    48.         return;
    49.     }
    50.  
    51.     public void CloseAD()
    52.     {
    53.         bannerView.Hide();
    54.         bannerView.Destroy();
    55.         Showing = false;
    56.         RequestBanner();
    57.         Debug.Log("Banner Hidden, Destroyed and a new one is requested.");
    58.         return;
    59.     }
    60. }
    61.  
    I randomly seem the get the Null error and I think its the reason my Banner is not being shown on my mobile it worked perfectly then it stopped showing, not sure at all why I'm getting the error:/
    Thanks in advance.
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello

    "NullReferenceException" means that the object you want to use is null (not assigned to an instance).
    Bannerad.manager.ShowBanner();
    --> in that case i guess manager is the issue. I see the manager = this assignement, but i can tell if the var is erased by a null value somewhere, or just never initialized because Bannerad Component is not in the scene. Follow that manager var and you should find where exactly is the issue.
     
    Venatal likes this.
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    It might be because the script is attached the the main camera in my main menu scene and the ShowBanner is called from my Level scene which does not have script attached the main camera but this worked fine before the update and I didn't read anything about that change?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you've loaded a new scene and that scene doesn't have the script in it, and the previous script isn't covered by a "dontdestroyonload" statement, then the instance has been destroyed by the scene load. As far as i know that is the same under "scenemanager" and "application" scene loads (i.e. new and old approaches).... it'd be interesting if it behaves differently now :eek:
     
  5. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    dang I just made another thread about 1 camera vs 2 where I talk about dontdestroyonload. Before the update I never used that and didn't get any null errors so I was confused but yeah I get the null error because the BannerView script is "destroyed"