10. Pause Menu

Now that our gameplay is finished, we'll start implementing the menus.

In this part, we'll create a simple pause menu that allows the player to resume, exit to the main menu or quit the game altogether. This menu will pause everything in the background while it is shown.

1. Reactivate the pause menu object

Remember how we deactivated the pause menu object back when we implemented the HUD loader? That was because we didn't want it to pollute our view all the time even when not in pause. Now that we'll implement a proper pause system, we can re-enable it.

  1. Open the HUD scene
  2. Select the PauseMenu object
  3. Reactivate it:

2. HUD state machine

We'll create a simple global HUD state machine with two states: Playing and Paused.

  1. Select the HUD object
  2. Add a new state machine component
  3. Set its source to Embed
  4. Rename the start state to Playing
  5. Add a new flow state called Paused
  6. Add transitions back and forth between the Playing and Paused states


2.1 Paused State

Open the Paused state. We will only use the On Enter State event to:

  1. Set the Unity time scale to zero, effectively pausing the game
  2. Activate the PauseMenu game object


2.2 Playing State

In the playing state, we will do the opposite: 

  1. Set the time scale back to one, effectively resuming the game
  2. Deactivate the PauseMenu game object


2.3 Pause Transition

To pause the game, we will wait for the the Escape keyboard button to be pressed:

2.4 Resume Transition

To resume the game, we will allow two options:

  • Press escape again
  • Click the resume button

One powerful feature of Bolt events is that they can be listened to on other objects than the current one. Here, we'll use this to our advantage and listen to the On Button Click event from the ResumeButton object. Simply drag & drop the ResumeButton object (under the PauseMenu / Buttons in the hierarchy) to the Self field to replace the target.

After renaming the transitions properly, the final HUD state graph should look like this:

3. Main menu button

The main menu button should simply load the Menu scene when clicked.

  1. Select the MenuButton object
  2. Add a new flow machine
  3. Set its source to Embed
  4. Add the following units:


4. Quit button

The quit button is equally simple: it just needs to quit the application altogether when clicked.

  1. Select the QuitButton object
  2. Add a new flow machine
  3. Set its source to Embed
  4. Add the following units:

Quit is located under Codebase > Unity Engine > Application. Note that it doesn't work while in the editor (i.e. it won't exit play mode), but it works while in a build.

That's it! You should now have a functional pause menu in every scene.

This article was helpful for 14 people. Is this article helpful for you?

Question.

If you got things happening in (in this case the HUD Scenes) that can not run directed as it errors. So you open the Level1 scene and run Unity. How do you go about debug problems. That is Leve1 is now running but something happens on HUD Scene. What do I do now to find problems (eg the Quit button is not working).- or am I missing something I should know in Bolt to find bugs

Thank you

Hi Wayne - I've had some luck looking at all my graphs while the game is running. You can usually find something is wrong there. But I agree that for a complex game, it is not a good debugging solution. The errors stacks seem useless to me... Anybody has a good way to debug?

Hi Wayne,

You should still be able to open the graphs from the HUD scene as you'll see the objects from it in the Hierarchy window on the left. In these graphs, the erroring nodes will be colored red as usual.

In future versions, an icon will be added to the hierarchy window to indicate when an object has an error on it.

As mentioned in this part, the Quit button doesn't work in the editor (Unity limitation), it only works in builds.

Hello ,

I hope someone will be able to help me. When I launch the game, the object "PauseMenu" deactivates (see from 1,20 min). I have tested many things. The console doesn't show an error.

+1

Hi Furkan
Where "2. HUD state machine" is inserted is not a PauseMenu game object.
Put it in the HUD game object.
(I use Google translation)

Mine kinda works? Like I still can pause and resume just fine with the Escape key, however whenever I try to unpause by hitting the "Resume" button, it unpauses the game, but takes 3 clicks before the pause menu goes away, anything I'm missing?