Unity3d-Finite-State-Machine
Unity3d-Finite-State-Machine copied to clipboard
How to correctly use Update and FixedUpdate?
Hi, I'm using this simple example like you described in Readme:
using MonsterLove.StateMachine;
using UnityEngine;
public class FSMTest : MonoBehaviour
{
public enum States { Init }
StateMachine<States> fsm;
void Awake()
{
fsm = new StateMachine<States>(this);
fsm.ChangeState(States.Init);
}
void Init_Enter() { Debug.Log("Init_Enter"); }
void Init_FixedUpdate() { Debug.Log("Init_FixedUpdate"); }
void Init_Update() { Debug.Log("Init_Update"); }
}
But the Update and FixedUpdate wont execcute. What am I missing?
Hi, I was struggling with that too. Look into the script 'StateMachineRunner.cs'. You will need this component to call the Update, FixedUpdate, LateUpdate methods.
I had the same issue which I solved by replacing
fsm = new StateMachine<States>(this);
fsm.ChangeState(States.Init);
by
fsm = StateMachine<States>.Initialize(this, States.Init);
This was the initialization step shown in an older version of the README. Not sure if this is the correct way to initialize now but it seems to work.
For everyone else who doesn't want to wade through code, the samples tell you how:
private void Update()
{
fsm.Driver.Update.Invoke(); //Tap the state machine into Unity's update loop. We could choose to call this from anywhere though!
}