The OnEnter method of the first state in a child state machine is always triggered when the parent state machine is created. I've made a minimal test setup to reproduce this. The child state machine is never actually entered, since the condition is never true. However we still get the log "Enter" once at the start, and never the "Exit" log.
internal class StateTester : MonoBehaviour
{
private StateMachine _stateMachine;
private void Start()
{
_stateMachine = new StateMachine();
_stateMachine.AddState(
"Start",
new State()
);
_stateMachine.SetStartState("Start");
StateMachine childStateMachine = new();
childStateMachine.AddState("Child Start", new TestGameState());
childStateMachine.SetStartState("Child Start");
childStateMachine.Init();
_stateMachine.AddState(
"Child",
childStateMachine
);
_stateMachine.AddTransition(new Transition(
"Start",
"Child",
x => false
));
_stateMachine.Init();
}
private void Update()
{
Debug.Log("Current: " + _stateMachine.ActiveState.name);//always logs the start state
_stateMachine.OnLogic();
}
}
internal class TestGameState : State
{
public override void OnEnter()
{
Debug.Log("Enter");//should not happen, but does
}
public override void OnExit()
{
Debug.Log("Exit");//never happens
}
}
The
OnEntermethod of the first state in a child state machine is always triggered when the parent state machine is created. I've made a minimal test setup to reproduce this. The child state machine is never actually entered, since the condition is never true. However we still get the log "Enter" once at the start, and never the "Exit" log.