Game States

Sometimes, you game changes context in a way that influences how everything works across the entire scene. For example, when a player is playing your game and they go to the pause menu, the game input and audio needs to pause, many different scripts need to temporarily stop running code, etc. Managing all of this can be difficult without organizing your game into clearly separated states.

With the Game State system, you can easily organize your game into distinct states and switch between them. The game can only be in one state at any given time. This makes it easy to understand what should be happening, and to know which code is supposed to be running and when.

Scene Setup

To begin, add a Game State Manager component anywhere in your scene.

This is the component that enforces and stores the current game state. It is a singleton, which means there must be only one of them in the scene at any time.

Creating Game States

To create a new game state:

  1. Go to the Game State Manager, open the inspector, and add an entry to the Game States list

  2. Go down to the Project tab and right click > Create > VSX > Game State and rename the file to something that describes the game state

  3. Drag the game state file you created into the Game State field in the inspector of the new game state you created in Step 1.

  4. Customize the rest of the values in the inspector.

You have successfully created a new game state you can switch to.

Setting The Game State

There are multiple ways to set the game state.

Starting Game State

To set the starting game state, which is the game state that the game will go to when the scene starts, set the Starting Game State field in the inspector of the Game State Manager inspector.

Changing Game State

To change the game state during gameplay, simply use the following code:

// Enter a new game state
GameStateManager.Instance.EnterGameState(someGameState);

Getting The Current Game State

To get the current game state, use the following code:

// Get the current game state
GameState currentGameState = GameStateManager.Instance.CurrentGameState;

Helpers

To help you easily manage different aspects of your game according to the current game state without having to write code, there are a couple of helper scripts you can use.

Game State Enabler

The Game State Enabler component provides an event that you to add functions to in the inspector to be called when a specific game state (or one of multiple 'compatible' game states) is entered.

To set it up:

  1. Add a Game State Enabler component on any object in the scene

  2. Set the list of 'Compatible Game States'

  3. Add your functions to the On Compatible Game State Entered and On Incompatible Game State Entered events

Game State Enter

The Game State Enter component is designed as a way to easily enter a new game state from a Unity Event, without having to link to the Game State Manager directly. This means, for example, that you can set up some code to enter a new Game State on a prefab that isn't in the scene.

To set this up:

  1. Add a Game State Enter component to the object

  2. Set the Game State you want to go to in the inspector

  3. Add the Game State Enter component's Enter Game State function to a Unity Event or call it from any script to enter the game state

Last updated