Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Objects appearing in inspector are null when debugging

Discussion in 'Scripting' started by Kent1227, May 11, 2024.

  1. Kent1227


    Jul 6, 2021
    I'm having trouble figuring out why a private variable of a class is coming up null even though the object is shown in the inspector. I'm trying to reset the position of these elements, but when the method runs, it says that the list is empty. If additional info is needed, please let me know, this is my first forum post. Below is the screenshot of the inspector showing the populated list. This variable is in the class "BoardManager" and there are no other references to this variable in any other class. BoardManager is not a singleton. The letter tokens are instantiated at runtime. No errors are thrown. The two images were taken at the same time.

    The variable is initialized as such:

    Code (CSharp):
    1. [SerializeField] private List<LetterToken> letterTokens;
    and the method that accesses this variable is below:

    Code (CSharp):
    1. public void ResetBoard()
    2. {
    3.     snapController = FindObjectOfType<SnapController>();
    4.     foreach (SnapNode node in snapController.snapPoints)
    5.     {
    6.         Destroy(node.gameObject);
    7.     }
    8.     foreach (var node in letterTokens)
    9.     {
    10.         node.gameObject.transform.position = node.startingPosition;
    11.     }
    12.     snapController.snapPoints = new List<SnapNode>();
    13.     GenerateRootSnapPoint();
    14. }
    I have verified that the code is being reached.

    inspector.png script.png
  2. Kurt-Dekker


    Mar 16, 2013
    Answers could be time-related, eg, null, you try to use it, then it's not null

    OR you could be looking at a different instance.

    Whatever it is, it will always be fixable by the standard same process:

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    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

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.

    "Stop playing 'Where's GameWaldo' and drag it in already!"

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information:

    More information:
  3. CodeSmile


    Apr 10, 2014
    Stomach feeling: it's about serializing runtime instantiated objects that are destroyed when exiting playmode. But that seems far off. More likely they have not been instantiated yet as ResetBoard runs.

    You have a debugger. Set breakpoints when instantiating and when resetting, see which one hits first.