Calling OnValidate on Awake with UNITY_EDITOR conditional compilation

In course Sam uses OnValidate function in ScriptableObject to build lookup tables.
OnValidate is not called during build/game, so we need to call OnValidate in Awake - like Sam mentioned.
But in course we have Awake in conditional compilation directive UNITY_EDITOR… sooo won’t it make not compile during build?

In Awake() we are creating one empty node, when we create instance of the DialogueNode.
Accoring to docs (Unity - Scripting API: ScriptableObject.Awake()):

Can we then loose the UNITY_EDITOR directive in this case?

Or maybe this way?

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

OnValidate() doesn’t need to run during play mode because the dialogue has already been created (by you) and has no reason to clear and recreate the lookups.

In general, though, even Awake() doesn’t run in play mode. The very next line of the documentation you pasted shows what you should do if you need to run initialisation code in play mode

1 Like

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

Privacy & Terms