Search Unity

Object Reference Error?

Discussion in 'Scripting' started by Art_Izon, May 29, 2019.

  1. Art_Izon

    Art_Izon

    Joined:
    May 29, 2019
    Posts:
    2
    So I'm not exceedingly new to coding, and am unsure why I'm having an issue here. I assume it's a quirk of Unity, as I am quite new to that.

    I have an input queue that I made for a fighting game project. I instantiate it here:

    Code (CSharp):
    1. public class Character : MonoBehaviour {
    2.  
    3.     InputQueue P1_Queue = new InputQueue();
    Then much further down I attempt to put something into the queue.

    Code (CSharp):
    1.  
    2.             QueuedInput new_input = new QueuedInput(5);
    3.             P1_Queue.insert(new_input); //this is the line where it throws the error
    4.         }
    Whenever I attempt to actually run the code, I receive an error reading "NullReferenceException: Object reference not set to an instance of an object."

    Unity claims my queue object doesn't exist -- I get the same error when I try to print properties it possesses (such as the front and rear) using Debug.Log(). This strikes me as odd, because I'm pretty sure that line at the top is sufficient to instantiate an object in C#. Is Unity different? If so, how? It seems like there is a difference between a "Unity Object" and an object in the general sense of code, and I'm unsure to which Unity refers when it gives me errors.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    That depends on how your InputQueue is defined. Might be something inside that is null.
    Also, if you have it set to public or if it have [SerializeField] it might be serialized to null.
    Which will override the constructor initialization code upon object deserialization (e.g. when your object is instantiated). (Check that via Debug inspector)

    Also, step in via an actual debugger, and see whats is the actual state of an object.
    Also, post your complete code. Otherwise its hard to help you.