I finished watching a couple tutorials on state machines in unity and I'm now trying to figure out how I can actually use one with my player movement code, but I'm a bit stuck. The tutorial I followed had shown to do a StateManager script like this
public class PlayerStateManager : MonoBehaviour
{
PlayerBaseState currentState;
public PlayerJumpState jumpState = new PlayerJumpState();
public PlayerIdleState idleState = new PlayerIdleState();
public PlayerRunState runState = new PlayerRunState();
void Start()
{
currentState = idleState;
currentState.EnterState(this);
}
void Update()
{
currentState.UpdateState(this);
}
public void SwitchState(PlayerBaseState state)
{
currentState = state;
state.EnterState(this);
}
}
And now I assumed all I would need to do to use this was this
public class TestMovement : MonoBehaviour
{
PlayerStateManager playerState;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
playerState.SwitchState(playerState.jumpState);
}
}
}
But this clearly isn't correct since I keep getting an error saying "NullReferenceException: Object reference not set to an instance of an object" at the line with SwitchState.