Visual Scripting "OnEnterState" being called before Monobehaviour "Awake"

I have a visual scripting graph with an OnEnterState trigger. The OnEnterState is set to show “VS Enter!” in the debug log (shown below). I also have a C# MonoBehaviour script with Debug.Log("C# Awake!"); in Awake(). For some reason, the visual scripting log is showing first in the console window, before Awake(). Is this expected behavior, or a bug with visual scripting?

Worth noting that there is a quick-fix for it. Deleting the object and recreating it causes MonoBehaviour’s Awake to be called first, but this seems like an unreliable fix.

Also worth noting that this is a reduced version of what was originally a much larger scene and state graph. I have not touched the script execution order.

I would update the visual scripting package but the package manager won’t let me go beyond 1.8.0 even though the project is 2023.2.4.

Any help is greatly appreciated.

It’s not available because 2023.2.4 is an old tech stream version of Unity and 1.8.0 release was latest Visual Scripting version available and validated at the time. You should Update Unity to Unity 6 preview, which will have the first “stable” release on October 17th in 4 days. Unity 2023 was rebranded to Unity 6, which is basically Unity 2023.3 equivalent. And the changelogs since 1.8.0 don’t mention anything about the StateGraph or OnEnterState event.

As for the actual issue, it might be a Unity engine related bug but I assume On Enter State is very likely called in Awake() itself. And there are no guarantees about a specific object’s Awake() executing before or after another object’s awake. I also can’t find any resources on UVS event execution order in relation to established Unity lifecycle events.

There are several ways to address this:

  1. Mark OnEnter state event as a Coroutine and add one of the time nodes to delay the execution. Easy but will cost some additional performance due to allocations caused by creating the coroutine.
  2. You could disable the StateGraph component, then have the Monobehaviour reference and enable it after its Awake() logic is done. As soon as the component is enabled, the state graph will work as usual.
  3. Edit script execution order for your Monobehaviour to execute before Visual Scripting graphs.