Adding A Pause Menu That Will Pause my Game.

Today, I implement a pause screen that can unpause the game or quit to the main menu. I do this by utilizing Time.Timescale.

Time.TimeScale

In short ,Time.TimeScale affects the speed of your application. Time.TimeScale is assigned a integer value that will determine the speed of your application.

Assigning Time.TimeScale to 1 will run our application at normal speed. Assigning it 0.5 will slow it down by half the speed and 0 will pause the application.

Setting Time.TimeScale to 0 will stop most things, but there are exceptions.

Time.TimeScale affects anything that is utilizing the Time class as well as animations. Meaning that anything that is being multiplied by Time.DeltaTime or Time.time will the affected. Update will continue to be called and executions that don’t interact with Time will continue to execute.

Currently, everything that moves in my game is being multiplied by Time.DeltaTime to be updated by time than by frame. So everything will pause when Time.TimeScale is set to 0, Perfect.

Assembling The Pause Menu UI

UIManager Prefab

I begin by creating a new canvas for my pause menu

Text to display that our game is paused.

If you do decide to animate it. Make sure to set the animator’s update mode to Unscaled Time. Unscaled time will allow it to play regardless of any changes in Time.TimeScale.

Then create three texts with button components.

Resume will call a newly created public method within our UIManager called UnPauseMenu(). The quit button will simply load a scene when pressed. Options wont do anything as of now, but it’ll eventually allow the player to adjust the game’s volume and other settings.

Pause Menu BackDrop in inspector
Pause Menu Back Drop In Game

I then create a Image to overlay over my screen. The image is a dark blue square with 80% transparency and was created in photo shop.

I go to my Quit button’s component and set its Onclick event to call a public function called SceneToLoad() within my scene manager. SceneToLoad() takes one integer parameter and passes that variable to SceneManager.Loadscene().

Whats cool is that unity will provide a input field that allows you to input data and pass it to your function. In the picture above, I was able to input 1 and pass it to my SceneToLoad() function. Which loaded scene 1 (main menu). Its super handy!

Finally, I disable the PauseMenuCanvas so it can be enabled when the game is paused.

Time to Jump Into Script

UIManager Script

I create two new reference to our player UI canvas and pause menu menu canvas so we can enable/disable through code. These references will be assigned accordingly via inspector.

PauseMenu() method will enable our pause menu canvas, disable our player ui, and set the time scale to 0.

gameObject.SetActive is used to enable/disable canvases. For some strange reason, .enabled is working for one of them. So I left it the way it is.

UnPauseMenu() method will only be called when the resume button is pressed. it does the inverse of PauseMenu().

Resume Button Component

I finish this off by linking the on click event on our resume button to our UIManager prefab’s UIManager script. Then locating my UnPauseMenu function in the drop down.

Player Script

I head over to my player script and do the following:

Create a PauseGame() Method that’ll check if the player has inputted the escape key, assign the IsPaused boolean to its opposite, and call the PauseMenu() function through our reference to our UIManager named PUI.

Disable player controls by using a boolean called ,’IsPaused’.

our reference to the UIManager is declared along side a private boolean named IsPaused. IsPaused will stop the player from executing any inputs while the game is paused while PUI is used to call the PauseGame() method.

At start, PUI is assigned a local game object variable named,’FindPlayerUI’ which locates the UIManager game object in our scene.

If the UIManager game object exists in scene, FindPlayerUI will be assigned our UIManager gameobject. FindPlayerUI can now be used to get the UIManager script component from the game object.

Ending it off with PUI being assigned the UIManager’s script component. Enabling us to be able to call any of its public functions.

Player Script

Our PauseGame() method will first check if the player has inputted the escape key. If the player has inputted escape key then it’ll set our IsPaused bool to the opposite of what it currently is.

In this case, this boolean is set to false by default so it’ll be set to true. Then it’ll pass right through the second if statement that checks if IsPaused is true. From here, it’ll communicate to our UIManager script, calling our newly created PauseMenu() function.

Player Script

This was done backwards because it proven to be more robust than checking if IsPaused was false and then setting the time scale to 1. I found that this executed without any issues.

Player Script

We then place the PauseGame() method in update and place all my player controls within an if statement that checks if game is not paused.

I can now pause my game whenever I want and quit my game whenever I decide I want to go outside.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vixian Phimmasone

Vixian Phimmasone

Unity Developer Based out of California