Search Unity

[SOLVED] Object reference not set to an instance of an object

Discussion in 'Scripting' started by u_rs, Mar 23, 2018.

Thread Status:
Not open for further replies.
  1. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    I'm trying to add listener to a button:
    Code (CSharp):
    1. void Start () {
    2.         ...
    3.         Button addButton = this.GetComponent<Button>();
    4.         addButton.onClick.AddListener(AddButtonClicked);
    5.     }
    And getting this:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
     
  2. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    addButton is null. check you added the Button component
     
  3. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    I did.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Check if addButton is null in code. If it is, then you either have an extra copy of the script on something, or you have something setup wrong in your scene.

    If neither of those is the case, then the error message is pointing towards another line.
     
  5. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    It is NULL. Still can't understand why.
     
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    If GetComponent<Button>() is failing, then you don't have a Button component on your gameobject.

    Is it in a child gameobject? If so, you should use GetComponentInChildren<Button>() instead.

    GetComponent<Button>() only works if your script component and Button component are on the same game object.
     
    MortRoe likes this.
  7. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    I have Button and script on it, I even checked if there Button component on the Button.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Also, make sure you don't have another object in the scene with the script on it and no button component.
     
  9. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    It is not null.
    This gives nothing:
     if(addButton == null) print("addButton is NULL!!!!!!!");


    And
    print(addButton);
    gives this:
    AddButton (UnityEngine.UI.Button)
    UnityEngine.MonoBehaviour:print(Object)
    AddItem:Start() (at Assets/AddItem.cs:24)
     
  10. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Is AddButtonClicked a method or a delegate that gets assigned a method? If the former, check it's not null either:
    Code (csharp):
    1. print(AddButtonClicked==null?"Delegate not assigned.":"Delegate assigned.");
     
  11. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    It's just a method.
     
  12. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    At this point you're going to have to share your project, or at least a trimmed down version of your project because there's only so much anyone can do with the info you're drip feeding out.
     
    u_rs likes this.
  13. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Sanity check: Click once on the error in the console and ensure the object that's throwing the exception has a Button component (it may not be the object you think it is). It highlights yellow in the hierarchy when you click on it.
     
    vetdetmett and u_rs like this.
  14. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Scabbage thanks a lot :D, I did as you said.
    The problem was that Text on Button had script attached.
    Thanks everybody.
     
    GorillaGums likes this.
  15. MortRoe

    MortRoe

    Joined:
    Feb 12, 2022
    Posts:
    2
    THANK YOU!!!
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
Thread Status:
Not open for further replies.