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

NullReferenceException

Discussion in 'Scripting' started by unity_1236alman, Mar 21, 2019.

  1. unity_1236alman

    unity_1236alman

    Joined:
    Mar 4, 2019
    Posts:
    66
    Code (CSharp):
    1.  public Button btnInventory1; //assigned in the inspector with a button with the same name
    2. public void Start()
    3.     {
    4.         btnInventory1.onClick.AddListener(OnclickbtnInventory1);
    5.     }
    6. public void OnclickbtnInventory1()
    7.     {    
    8.         if (pnlInventory1.activeSelf)
    9.         {
    10.             pnlInventory1.SetActive(false);
    11.         }
    This is the full error :"NullReferenceException: Object reference not set to an instance of an object
    AuctionManager.Start () (at Assets/AuctionManager.cs:70)" at cs:70 i have this "btnInventory1.onClick.AddListener(OnclickbtnInventory1);"
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Just so you know, the button doesn't need to have the same name. It should just be dragged and dropped into the slot.

    However, if line 4 above is where your null is, then the value of btnInventory1 is null. So either you don't have it connected properly or you have a duplicate of the script in your scene and that one doesn't have a value assigned to it.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    NullReferenceException means that you are trying to do something using a variable that has a value of null. In this context, the most likely scenario is that you haven't assigned a value to btnInventory1.
     
  4. unity_1236alman

    unity_1236alman

    Joined:
    Mar 4, 2019
    Posts:
    66
    Ok i found that, i inherited a class and in the inspector there are all the variables (that are the same obviously) null. Why they didnt take the value of the class they inherited?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I'm a bit unclear--where exactly were you expecting the variable
    btnInventory1
    to get its value from?

    I'm not completely sure of this, but my understanding is that Unity calculates "default values" for all the fields in the inspector by invoking your class's constructor and then remembering what values they had when it finished. (Of course, once it's done this for a given variable, it then remembers the value it had in the inspector, and will continue to use that value even if the constructor changes.)

    Normally when you inherit a class, the derived class's constructor will implicitly call the base class's default constructor (unless you specify otherwise). So you should keep any initializations that the base class would have had unless you do something to overwrite them.

    But types like Button generally can't be set to a reasonable value (other than null) from inside the constructor, because you can't count on the rest of the world to exist when the constructor runs. So normally you need to set them manually in the inspector, OR rely on runtime initialization in functions like Awake() or Start() (which will overwrite any value that you set in the inspector).
     
  6. unity_1236alman

    unity_1236alman

    Joined:
    Mar 4, 2019
    Posts:
    66
    Thank you!