Search Unity

Object reference not set to an instance of an Object

Discussion in '2D' started by unity_4xHAL1ncP-he2g, Jun 25, 2019.

  1. unity_4xHAL1ncP-he2g

    unity_4xHAL1ncP-he2g

    Joined:
    Jun 25, 2019
    Posts:
    2
    Hey guys! I am pretty new to C# and am currently making my second game using this language. The game is a clone of Bayat and I have been doing really well so far but I am stuck as an error is being shown in the console.
    My Death Field Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. using RedRunner.Collectables;
    6. using System;
    7.  
    8. namespace RedRunner.UI
    9. {
    10.     public class UICoinText : UIText
    11.     {
    12.         [SerializeField]
    13.         protected string m_CoinTextFormat = "x {0}";
    14.  
    15.         protected override void Awake ()
    16.         {
    17.             base.Awake ();
    18.         }
    19.  
    20.         protected override void Start()
    21.         {
    22.             GameManager.Singleton.m_Coin.AddEventAndFire(UpdateCoinsText, this);
    23.         }
    24.  
    25.         private void UpdateCoinsText(int newCoinValue)
    26.         {
    27.             GetComponent<Animator>().SetTrigger("Collect");
    28.             text = string.Format(m_CoinTextFormat, newCoinValue);
    29.         }
    30.     }
    31. }
    and also my UICoinImage Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. using RedRunner.Collectables;
    7.  
    8. namespace RedRunner.UI
    9. {
    10.  
    11.     public class UICoinImage : Image
    12.     {
    13.  
    14.         [SerializeField]
    15.         protected ParticleSystem m_ParticleSystem;
    16.  
    17.         protected override void Awake ()
    18.         {
    19.             base.Awake ();
    20.         }
    21.  
    22.         protected override void Start()
    23.         {
    24.             GameManager.Singleton.m_Coin.AddEventAndFire(Coin_OnCoinCollected, this);
    25.         }
    26.  
    27.         void Coin_OnCoinCollected (int coinValue)
    28.         {
    29.             GetComponent<Animator> ().SetTrigger ("Collect");
    30.         }
    31.  
    32.         public virtual void PlayParticleSystem ()
    33.         {
    34.             m_ParticleSystem.Play ();
    35.         }
    36.     }
    37. }
    The exact error message is here:
    NullReferenceException: Object reference not set to an instance of an object
    RedRunner.UI.UICoinText.Start () (at Assets/Scripts/RedRunner/UI/UICoinText.cs:22)
    and also :
    NullReferenceException: Object reference not set to an instance of an object
    RedRunner.UI.UICoinImage.Start () (at Assets/Scripts/RedRunner/UI/UICoinImage.cs:24)
    Thanks to anyone who can help :)
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    A NullReferenceException means you are trying to access a reference to an object that has not been assigned. IE: It doesn't exist.

    The error messages point to which lines in which scripts are throwing the error:

    "NullReferenceException: Object reference not set to an instance of an object
    RedRunner.UI.UICoinText.Start () (at Assets/Scripts/RedRunner/UI/UICoinText.cs:22)"

    • The error is on line 22 in the script named "UICoinText.cs".

    "NullReferenceException: Object reference not set to an instance of an object
    RedRunner.UI.UICoinImage.Start () (at Assets/Scripts/RedRunner/UI/UICoinImage.cs:24)"

    • The error is on line 24 in the script named "UICoinImage.cs".
    In both scripts, the lines throwing the error are
    GameManager.Singleton.m_Coin.AddEventAndFire(UpdateCoinsText, this);
    .

    You have two references in this line:
    • Singleton
    • m_Coin
    One or both of these references have not been assigned. Check back to where you declare them and make sure to assign them.
     
    Last edited: Jun 25, 2019
    DaemonicDreams likes this.
  3. DaemonicDreams

    DaemonicDreams

    Joined:
    Aug 23, 2018
    Posts:
    41
    Since you are new to C# (and judging by the question I guess to coding itself) I want to expand on Vrykens excellent answer a bit:

    A "reference" is like an "adress" or a "pointer" to some object in memory, like an arrow that tells you where to go.
    An "instance" is basically the stuff that is in memory at that location.

    Memory management and pointers (or references) are sometimes a somewhat cryptic subject, but the basics are quite simple:

    This is how you create a reference:
    Code (CSharp):
    1. GameObject m_Object;
    "GameObject" defines the type (or class) of the object behind your reference and "m_Object" is the reference itself.

    After this line, you have not created an object. You just defined a way for your script to access an object. Basically, you've put up a "sign" that says "GameObject that way ->", but it points to nowhere ("nowhere" is basically what "null" means).

    This is how you create an instance:
    Code (CSharp):
    1. m_Object = new GameObject();
    After this line, this happened:
    1. The "new" keyword "told your program" to get some space in memory for an object (or instance) of the type (or class) "GameObject"
    2. "GameObject()" calls the constructor method of the GameObject class
    3. The actual adress to the memory space that "new" created is put into "m_Object" - your sign now actually points somewhere!

    If you now access m_Object, your program "reads" the sign and knows where to go to find the GameObject instance in memory.

    There's more to it and internally it's a little more complicated, but this are the basics of what is happening.
    What happened in your code is what Vryken already said: You've put up the sign, but you've never told the sign where to point.
     
    Vryken likes this.