Unity3d-Finite-State-Machine icon indicating copy to clipboard operation
Unity3d-Finite-State-Machine copied to clipboard

How to correctly use Update and FixedUpdate?

Open omid3098 opened this issue 4 years ago • 3 comments

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?

omid3098 avatar Aug 21 '21 15:08 omid3098

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.

LeeHyeonJae97 avatar Sep 09 '21 13:09 LeeHyeonJae97

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.

Barbelot avatar Mar 28 '22 10:03 Barbelot

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!
}

wes-kay avatar Dec 02 '22 04:12 wes-kay