Search Unity

[UNSOLVED] Mysterious 'Object reference not set to an instance of an object' error

Discussion in 'Scripting' started by xolotlll, Aug 1, 2017.

  1. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Hey all,

    I'm getting the following error and I can't work out why. It doesn't seem to impact the game at all - everything works, it's just annoying.

    NullReferenceException: Object reference not set to an instance of an object
    PlayerMovement.OnGUI () (at Assets/Scripts/PlayerMovement.cs:177)

    The error relates to the line that references actualDialogueLength - I've written XXXXX to the side. If I delete that part of the code, the same error comes up for the following two lines that reference actualDialogueLength.

    Code (CSharp):
    1. public static int actualDialogueLength;
    2. public static string currentDialogue;
    3. public static string actualText;
    4.  
    5.     void OnGUI ()
    6.     {
    7.         //set dialogue text
    8.         if (actualDialogueLength < currentDialogue.Length) //XXXXX
    9.         {
    10.             actualDialogueLength += 1;
    11.         }
    12.  
    13.         //draw the underscore
    14.         if (underscore == 0)
    15.             actualText = currentDialogue.Substring(0, actualDialogueLength);
    16.         else
    17.             actualText = currentDialogue.Substring(0, actualDialogueLength) + "_";
    18.  
    19.     }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    try this
    public static int actualDialogueLength = 0;
     
  3. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Thanks for the suggestion. I've just tried that and it throws the same error, unfortunately.
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you post the error
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    fyi
    OnGui() is old and should not be use in my opinion.
     
  6. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    The OnGui() is executing before one of these are set. so it's null

    do you have an Awake() function or Start() function?
    if not create one.
    put actualDialogueLength = 0;
    and
    currentDialogue = "";


    ie:
    Code (CSharp):
    1. void Start()
    2. {
    3. actualDialogueLength = 0;
    4. currentDialogue = "";
    5. }
     
    cstooch likes this.
  8. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    I don't know why you'd get an error for the int, as that should default to 0 if you didn't populate it, I believe (it least it does when I test outside of Unity in C#.. I can't test in Unity currently), but doing Length on a string you didn't initialize would give that NullReference exception though.

    Do something like this in your if, and/or initialize your string/int:

    Code (csharp):
    1. if (!string.IsNullOrEmpty(currentDialogue) && actualDialogueLength < currentDialogue.Length)
    Edit: Didn't see John's post before mine, that would solve the issue as well.
     
    Last edited: Aug 1, 2017
    Habitablaba likes this.
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In my experience, this usually means that your script exists on an object you didn't expect it to - it got added to it at some point by mistake. And those string variables will be null until the component is viewed in the Inspector, which would explain your issue.

    Search for your component in your Hierarchy, I guarantee there's a stray one hanging out causing this error.
     
    cstooch likes this.
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Agree that currentDialogue is most likely where the error is. Ints that aren't nullable can't be null, and since you say the error is on all the lines where currentDialogue is, that would seem to hold true. So as suggested, set a default value of an empty string.
     
  11. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Great suggestion.

    BTW, for the OP, I find that right clicking on your script in the Project Explorer and selecting Find References in Scene (I think that's what it says?? I don't have Unity on this PC) makes it easy to find anywhere where your script has been attached.