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.
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.
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.
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.
There are multiple ways to set the 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.
To change the game state during gameplay, simply use the following code:
// Enter a new 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.
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.
Simply add a Game State Enabler 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.
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.
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 Game State Enter component to the object, set the game state you want to go to, and add its 'Enter Game State' function to the Unity Event on some other component.