Objects appearing in inspector are null when debugging

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:

[SerializeField] private List<LetterToken> letterTokens;

and the method that accesses this variable is below:

public void ResetBoard()
{
    snapController = FindObjectOfType<SnapController>();
    foreach (SnapNode node in snapController.snapPoints)
    {
        Destroy(node.gameObject);
    }
    foreach (var node in letterTokens)
    {
        node.gameObject.transform.position = node.startingPosition;
    }
    snapController.snapPoints = new List<SnapNode>();
    GenerateRootSnapPoint();
}

I have verified that the code is being reached.

9828666--1413363--inspector.png 9828666--1413366--script.png

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

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

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™ 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: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

More information: https://discussions.unity.com/t/899843/12

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.