Anyone else getting a Null Reference upon changing to node.name?

So once we made the switch from uniqueID to name I would get a null reference error immediately after creating a new dialogue. Interestingly, everything would still work but it would throw the error. It was the call to OnValidate in Awake() and pointed to:

    private void OnValidate()
    {
        //  Called anytime things change in editor and on startup. Clear and rebuild dictionary by UID and DN.
        //! See Awake() below for important addition to this.
        nodeLookup.Clear();
        foreach (DialogueNode node in GetAllNodes())
        {
           nodeLookup[node.name] = node; <---- throws here
        }
    }

I went ahead and added a null check there. I am not sure why it would be null right away unless it is a script order race situation. Unity jumping the gun on getting that info before it is populated. Seems to get rid of the error and so far no weird behavior but I’ll keep an eye out.

    private void OnValidate()
    {
        //  Called anytime things change in editor and on startup. Clear and rebuild dictionary by UID and DN.
        //! See Awake() below for important addition to this.
        nodeLookup.Clear();
        foreach (DialogueNode node in GetAllNodes())
        {
            if(node != null) nodeLookup[node.name] = node;
        }
    }

(also notice my obsessive noting about OnValidate not being called in the compiled game… well that is what my note down in Awake says, this is just a reminder to pay attention to that :joy: )

So what’s happening here is a race condition between the dialogue creation and OnValidate. I believe Sam addresses this in a later video.

1 Like

Privacy & Terms