I have a state machine that works for the most part. Problem is each state needs a function to run only once, only when the state is first switched to.
So for example, when the idle state switches to the move state, I want to run a function in the move state but only once.
My absolute bare bones code:
State:
public abstract class State : MonoBehaviour {
public abstract State RunCurrentState();
}
State Manager:
// I used this tutorial: https://www.youtube.com/watch?v=cnpJtheBLLY&ab_channel=SebastianGravesSebastianGraves
public class StateManager : MonoBehaviour {
public State currentState;
void Update() {
RunStateMachine();
}
private void RunStateMachine() {
State nextState = currentState?.RunCurrentState();
if (nextState != null) {
SwitchToTheNextState(nextState);
}
}
private void SwitchToTheNextState(State nextState) {
currentState = nextState;
}
}
Move State:
public class MoveState : State {
public override State RunCurrentState() {
return this;
}
}
Hierachy/Inspector:
So in the MoveState public override State RunCurrentState()
is run every frame.
If I put a start method in the MoveState it is run when the prefab first loads, I actually want to run some code when the State is first switched to.
Also I’m still a beginner, I’m sure there are far better ways of implementing this with ScriptableObjects or something but for now if someone could just help me with this issue that would be fantastic!
Many Thanks!