Search Unity

Question Invoking UnityEvents from within a StateMachineBehaviour is not working (all members are null)

Discussion in 'Animation' started by PeterPudeltreter, May 16, 2021.

  1. PeterPudeltreter

    PeterPudeltreter

    Joined:
    Apr 1, 2021
    Posts:
    9
    What I'm trying to do:

    Since I can't simply ask an Animator if a state machine is inside a certain Substatemachine or not, I wrote a StateMachineBehaviour script that monitors the access and triggers events when entering/exiting the monitored states (I need to manually update a string list by hand with the names of the monitored states and I don't think there is a better/easier solution).

    My problem:

    I subscribe to these events from within another StateMachineBehaviour script that sits on the same State. However when the callbacks are exectuted, all members inside the callback are uninitialized/null.

    This is how I invoke the events:

    Code (CSharp):
    1. public class SubstatemachineMonitorAccess : StateMachineBehaviour
    2. {
    3.     public SubstatemachineMonitorEvent OnSubstatemachineEntered;
    4.     public SubstatemachineMonitorEvent OnSubstatemachineExited;
    5.  
    6.     [SerializeField] private List<string> monitoredStatesInsideSubstatemachine;
    7.  
    8.     [System.Serializable]
    9.     public class SubstatemachineMonitorEvent : UnityEvent<Animator> { }
    10.  
    11.     private readonly List<int> monitoredStatesHashes = new List<int>();
    12.     private bool active = false;
    13.  
    14.     private void Awake()
    15.     {
    16.         foreach (string state in monitoredStatesInsideSubstatemachine)
    17.         {
    18.             monitoredStatesHashes.Add(Animator.StringToHash(state));
    19.         }
    20.     }
    21.  
    22.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    23.     {
    24.         int stateHash = animator.GetNextAnimatorStateInfo(layerIndex).shortNameHash;
    25.         if (!active && monitoredStatesHashes.Contains(stateHash))
    26.         {
    27.             OnSubstatemachineEntered.Invoke(animator);
    28.             active = true;
    29.         }
    30.     }
    31.  
    I register the callback functions that are defined in another StateMachineBehaviour script that sits on the same state. I do this through the editor (the Inspector dropdown menus that were created for the public SubstatemachineMonitorEvents).

    But inside that callback, everything is uninitialized:

    Code (CSharp):
    1. public class KombatState : StateMachineBehaviour
    2. {
    3.     private string test;
    4.  
    5.     private void Awake()
    6.     {
    7.         Debug.Log("Awake");
    8.         test = "Test";
    9.     }
    10.  
    11.     public void OnSubstateMachineEnter(Animator animator)
    12.     {
    13.         Debug.Log("Callback");
    14.         Debug.Log(test);
    15.     }
    16. }
    17.  
    The test variable is not initialized, even though Awake has been called before the callback is executed.

    Am I doing something wrong or missing something?
     
    Last edited: May 16, 2021