← All posts
postMay 4, 2026
State Machine Pattern in C#
#csharp#unity#state-machine#patterns

csharp
public interface IState
{
void Enter(); void Tick(); void Exit();
}
public class StateMachine
{
private IState _current;
public void SetState(IState next)
{
_current?.Exit();
_current = next;
_current?.Enter();
}
public void Tick() => _current?.Tick();
}
// Usage
public class IdleState : IState
{
public void Enter() => Debug.Log("Entering Idle");
public void Tick() { /* check transitions */ }
public void Exit() => Debug.Log("Leaving Idle");
}