Search code examples
unity-game-engine

How to Have State Graphs Listen to MonoBehaviour Events


I'm trying to program an enemy for my game using State Graph and some MonoBehaviours I wrote that, among other things, invoke UnityEvents under certain circumstances (when the player enters the enemy's field of view, when the enemy touches the ground, etc.). However, I can't figure out how to have parts of the Script Graphs run once these events are invoked.

I have confirmed that the MonoBehaviours are invoking the relevant events when intended, either because other MonoBehaviours that listen for them work, or because booleans that get set to true when and only when the event is invoked are getting set to true. I'm trying the follow the "Unity Events" section of this article to start flows (not sure if that's the correct term) inside the Script Graphs of the State Graph's transitions. I've added Debug: Log nodes to the Script Graphs such that messages should be printed to the console when the right UnityEvent is invoked, but nothing seems to be happening when the events should be invoked.

As an example, here is the Inspector of the enemy's prefab:

enter image description here

The "Saw Target" event is invoked when the player enters the enemy's FOV. Here's the enemy's State Graph:

enter image description here

And here's the Script Graph in the transition between the "Wandering" and "Surprised" states:

enter image description here

I'm expecting at least that Debug: Log node at the top to trigger, and print "Saw Fella" in the console, when the "Saw Target" event is invoked, but no such thing is happening. Could anyone explain what I'm doing wrong?


Solution

  • I managed to get this working by following this tutorial (in my case, renaming the "MyCustomEvent" symbol to "SawTarget"), and then calling the line

    EventBus.Trigger(EventNames.SawTarget, 0);

    after the line where I invoke the event other MonoBehaviours use. No idea what that UnityEvent node is for when this works, but there you go.