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.
The Game State System enables you to easily organize your game into states, seamlessly managing many different aspects across the entire scene.
With the Game State system, you can create any number of different states and switch between them. The game can only be in one Game State at any given time. This makes it easy to organize things and to know which code is supposed to be running and when.

Game State Manager

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

Each game state is represented by a Game State object, which is a scriptable object that you create in your Assets folder.
To create a new Game State, right click anywhere in the Project tab in the editor and select Create > VSX > Game State. This will create a new Game State in your Assets folder which you can rename.

Setting Up The Game State

Next, go to the Game State Manager you added to your scene and add a new entry to the 'Game States' list in the inspector. Drag the Game State you created above into the 'Game State' field, and adjust the other settings as required.
In the inspector, you can set things like:
  • The Time Scale for the game state
  • A pause before entry into the game state
  • Whether to center, lock, and/or show the mouse cursor
  • Whether to pause the Audio Listener in the game state
  • Restrict which other game states a game state can be entered from
You have successfully created a new game state that 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, drag a Game State object into 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, with a reference to the game state you wish to switch to, in any script:
// Enter a new game state

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;


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 allows you to add functions to be called when a specific game state (or one of multiple 'compatible' game states) is entered, right in the editor.
Simply add a GameStateEnabler component on any object in the scene, set the list of 'Compatible Game States', and add your functions to the 'On Compatible Game State Entered' and 'On Incompatible Game State Entered' events. This component will automatically connect to the Game State Manager in the scene and run the events.

Game State Enter

The GameStateEnter 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.
Usually, there would be a Unity Event on some component, and when this event is called, you want to enter a game state. Simply add the GameStateEnter component to the object, set the game state you want to go to, and add its EnterGameState function to the Unity Event.
Last modified 24d ago