To confirm the correction pointed out by Sam from the future

I would like to confirm if I understand what Sam from the future was mentioning in the video:

#if UNITY_EDITOR
        private void Awake() {
            if (nodes.Count == 0)
            {
                nodes.Add(new DialogueNode());
                OnValidate();   //<--Call OnValidate here?
            }
        }
#endif
2 Likes

You’re actually going to want to call OnValidate regardless of whether there are existing nodes, since OnValidate is where we build the Dictionary.

A couple changes, though, to make sure that you can run your project after it’s been built. It turns out that OnValidate is never called in a built game (because it’s generally called when editing). For this reason, we need to make sure that OnValidate is called in Awake even when it’s not editor…

Make sure that OnValidate() is not in an #if UNITY EDITOR block.
Then make Awake look like this:

private void Awake()
{
     #if UNITY_EDITOR
            if (nodes.Count == 0)
            {
                nodes.Add(new DialogueNode());
            }
     #endif
     OnValidate();
}

Later in the section, we’ll be changing the way we add that default node. When that happens, Sam will be deleting the Awake() method to cover the OnBeforeSerialize method.
In this case, rather than deleting Awake(), just delete the section in the editor block. Then Awake will be simply

private void Awake()
{
     OnValidate();
}
7 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms