State Machine Hell

Okay so, I came across a nice little fix for my menu and start game problem in my coding. The problem is that I got errors coming from every orifice mostly the top half. This is the template I had:

And Unity was loving it for the first few bits. Any tips would help and other than the StateMachine I got it all word for word.

class StateMachine {
    Map<String, IState> mStates = new Map<String, IState>();
    IState mCurrentState = EmptyState;
    StateMachine gGameMode = new StateMachine();

    gGameMode.Add("mainmenu", new MainMenuState(gGameMode));
    gGameMode.Add("openingscrean", new OpeningScreanState(gGameMode));
    gGameMode.Add("localmap", new LocalMap(gGameMode));


    public void Update(float elapsedTime) {
    public void Update() {
        float elapsedTime = GetElapsedFrameTime();
    public void Render() {
    public void Change(String stateName) {
        mCurrentState = mStates[stateName];
    public void Add(String name, IState state) {
        mStates[name] = state;

As it turns out I could have just looked at that tutorial. Look at the top…

So that code you typed in is “Psudo JS” otherwise known as BS!

I’d find a different tutorial to go off if I were you and if possible try one that’s C# unless you already know a fair bit JS. C# is faster in unity and if you’re learning a new language anyway C# will be better as the unity tutorials are in that.

EDIT - got a bit of time to look and THIS looks pretty good if you’re trying a 2D RPG style game.


Finite State Machine (FSM) can quickly become a hell to manage as the number of states is growing. Because the number of transitions grows quadratically and any modification to the state machine requires to fiddle with the transitions, which is tedious and error prone.

If you are looking for an alternative to FSM, have a look at Behaviour Tree, which is far more flexible and easier to manage.

I’m the author of Panda BT (, it’s a script based behaviour tree engine.

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