state machine countdown timer

¡Hola! I’m still learning so please bear with me.

i recently watched the live training for state machines link: http://unity3d.com/learn/tutorials/topics/scripting/using-interfaces-make-state-machine-ai

i am trying to modify this code for my own purposes but have come across a stumbling block. i have tried to make a simple countdown time to display using the new UI system but when i play all it displays is the original timeRemaing. for some reason it just doesn’t seem to count down can anyone help? here is my code so far.

public class StatePatternGameplay : MonoBehaviour {

[HideInInspector] public IGameplayState currentState;
[HideInInspector] public CountdownState countdownState;

public Text countdownText;
public float timeRemaining = 5f;

private void Awake()
{
	countdownState = new CountdownState (this);
}
	
void Start () 
{
	currentState = countdownState;
}
	
void Update () 
{
	currentState.UpdateState ();
}

and

public class CountdownState : IGameplayState {

private readonly StatePatternGameplay gameplay;

public CountdownState (StatePatternGameplay statePatternGameplay)
{
	gameplay = statePatternGameplay;
}

public void UpdateState()
{
	gameplay.timeRemaining -= Time.deltaTime;

	gameplay.countdownText.text = Mathf.Round (gameplay.timeRemaining).ToString();
}
	

public void ToCountdownState()
{
	Debug.Log ("Can't transition to same state");
}

Hi,

Do you have only on state defined? Is currentState assigned to countdownState and stay so?

Otherwise, I don’t see why this code is not working.

Finite State Machine is a very common tool. Despite that it is simply to understand and can become cumbersome to use.

I suggest to learn about Behaviour Tree. It’s a good alternative to FSM and is easier to use on the long run.

Have a look at Panda BT (www.pandabehaviour.com), it’s a script-based Behaviour Tree framework.

If you have any question about using this tool, you’re welcome on this thread.

After much pain of trying to figure out the issue I decided to use this state machine and just import each of the scripts into the new state machine testing each stage