[Video][Unity StateMachine Tutorial 1] UI Handling Part II

[Part I]

After we have our game setup with several UI views, we can start coding the State Machine in. It’s going to be pretty straightforward but it’ll teach you guys how to handle this type of UI handling.


This text part is going to explain how it actually works.

First, in your variable area, you need to set this up:

public enum GameState { Statename1, Statename2 , }


This way, the code recognizes all your states are linked with each other. Now, each state will contain a set of instructions. As I set it up, the game is stuck “in the middle” of the state until the state is changed. When it is, it runs “the end” part of the state, thus closing in and moving successfully to the next.


IEnumerator Statename1(){


while (currentState == GameState.Statename1){

yield return null;


End instructions;



We also need a function that allows us to change the state at a whim. This is:


public void ChangeState(GameState newState){

currentState = newState;




With those set up, we are able to change between states and just in general use our statemachine. I’ll recommend watching the video in detail and asking any questions you have so I can give you a hand.


Thanks for reading!



Trackbacks & Pings

Leave a Reply

Your email address will not be published. Required fields are marked *