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

Question reference becomes null when passed into method

Discussion in 'Scripting' started by SushiFanta, Jul 18, 2023.

  1. SushiFanta

    SushiFanta

    Joined:
    Oct 5, 2021
    Posts:
    2
    I have some code where a script is supposed to add itself to an array in a singleton, like so:

    Initialize() in Structure
    Code (CSharp):
    1. public void Initialize()
    2.     {
    3.         gameObject.layer = LayerMask.NameToLayer("Structure");
    4.         if(this == null)
    5.         {
    6.             UnityEngine.Debug.Log("this is null");
    7.         }
    8.         GridMgr.gridMgr.Add(this);
    9.     }
    Add() in GridMgr
    Code (CSharp):
    1. public void Add(Structure structure)
    2.     {
    3.         if (structures == null)
    4.         {
    5.             UnityEngine.Debug.Log("structures is null");
    6.         }
    7.         structures.Add(structure);
    8.     }
    The issue is that "this" is not null before it is passed into Add(), but Structure is null within Add(). Why is this value being nulled? I am not messing with the values in any other scripts that might be running concurrently, although I don't think that matters because this shouldn't be multithreaded anyways.

    upload_2023-7-17_15-23-25.png
     
  2. SushiFanta

    SushiFanta

    Joined:
    Oct 5, 2021
    Posts:
    2
    nevermind
     
    Sluggy likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    S'all guud sir... lists need to be initialized too. That's why I have my three step checklist.

    In your case you hadn't actually found the thing that was null... until you did. :)

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    That's why I patiently tell folks who insist that something isn't null:

    "That's great, it just means you haven't found the thing that IS null. Keep looking!"
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Just in case someone comes across this thread:
    This
    structures
    is not the same as
    structure

    :) So make sure you actually use the correct variable. I personally usually prefix "arguments" of methods with an "a" (for argument). This avoids name collisions with member variables and also makes it easier to recognise it as an argument. However different people, different styles.

    Code (CSharp):
    1. public void Add(Structure aStructure)
    2. {
    3.     if (aStructure == null)
    4.         UnityEngine.Debug.LogError("passed Structure 'aStructure' is null");
    5.     if (structures == null)
    6.         UnityEngine.Debug.LogError("'structures' List is null");
    7.     structures.Add(structure);
    8. }