Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Dragonbornbard, Oct 27, 2020.

  1. Dragonbornbard

    Dragonbornbard

    Joined:
    Oct 27, 2020
    Posts:
    3
    I haven't used Unity in years and tried to hop back into things.

    I'm trying to have a script that both increases the variable by one by clicking a button and shows that variable in a text box.

    The textbox part was what was shown on one of the official Unity GUI tutorial videos.

    Screenshot_1.png
    Now I'm getting 999+ errors for the highlighted line:
    NullReferenceException: Object reference not set to an instance of an object
    Clicking.Update () (at Assets/Script/Clicking.cs:29)
    But everything works as it should.

    I would very much appreciate any help on this
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    Always the same answer, ALWAYS the same for a nullref, the most common pedestrian error ever:

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.
     
    bobisgod234 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Usually when you get an error like this, but also everything seems to be working just fine, it's because you have an extra, broken copy of the script floating around your scene that you forgot about. In this case, you have a copy of the script attached to an object that does not also have a Text component on it.
     
  4. Dragonbornbard

    Dragonbornbard

    Joined:
    Oct 27, 2020
    Posts:
    3

    ...Yeah, I made this because I didn't understand why it was null.

    Thank you so much for this help.
     
  5. Dragonbornbard

    Dragonbornbard

    Joined:
    Oct 27, 2020
    Posts:
    3
    Thank you very much, I missed something that I tested the script with before the text parts were added.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    Put Debug.Log() lines in where it is used (looks like the offending field is
    text
    ) in your code: where it is set, where it is used and gets a nullref. Since you have clear external linkages such as your ClickTheButton(), something might not be ready for business yet.