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. Dismiss Notice

Scripting Buttons: 'Object Reference not set to an instance of an object' error

Discussion in 'Scripting' started by jpconnel, Sep 22, 2020.

  1. jpconnel

    jpconnel

    Joined:
    Sep 21, 2020
    Posts:
    4
    Hey! I am trying to script a button, and keep getting a NullReferenceException error in Unity. I am following examples, and can't find what I am doing wrong. Any help would be greatly apprecipated!
     

    Attached Files:

  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Did you assign the ButtonThree reference in the inspector? A NullReferenceException basically tells you that some object reference (ie the Button object) was not set, but you are trying to use it.

    Also the line in the error message does not match the lines of your script. Did you by any chance remove empty lines between the screenshots? The line in question should be 20 or 21, there is no code at 22.

    In the future please use code tags instead of screenshots tho :)
     
  3. jpconnel

    jpconnel

    Joined:
    Sep 21, 2020
    Posts:
    4

    Thanks for the reply, yes you are right the error was in Line 20 (Line 9 below). I am not sure why this is not set, the script has been added to the GameObject, which has been added as an OnClick() event to the desired button. Here is the embedded code:

    Code (CSharp):
    1. public class negativePoint : MonoBehaviour
    2. {
    3.     public Button ButtonThree;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.  
    9.         Button btn3 = ButtonThree.GetComponent<Button>();
    10.         btn3.onClick.AddListener(NegativeClick);
    11.     }
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Did you drag the button script into the ButtonThree field in the inspector of your negativePoint script?

    Edit: Also ButtonThree is already type Button, no need to get its Button component. Not sure if that's even possible, so maybe that's a problem as well. Check what i wrote above before tho.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    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.
     
  6. jpconnel

    jpconnel

    Joined:
    Sep 21, 2020
    Posts:
    4
    It would not let me drag any button type into that box. I think you are right, it works much better when I don't access the Button component and just add the function to the button in the Inspector. Thanks! :)
     
  7. jpconnel

    jpconnel

    Joined:
    Sep 21, 2020
    Posts:
    4
    Thanks! That also helps a lot :)
     
    Kurt-Dekker likes this.