Fork me on GitHub

Pause Menu Tutorial

03 November 2020

So today we are going to be making pause menus in unity.

Currently I am using Unity 2019.4.10, So this can be applicable for future versions and maybe some older ones too.

I have a pre-prepared scene with only particles moving randomly.





Lets start by creating a C# script and call it GameManager.cs
so this is the default script you will get

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour{
        
        //Use..
        void Start(){
            
        }

        //....
        void Update(){

        }
    }



You can remove both the comments.
First create a canvas with all the elements to be displayed when the game is paused and disable it.
Rename the canvas to PauseCanvas or what you choose.

We will use Time.timeScale to change the active state of the game.
When its value is 0 then the game will like stop and if it is 1 it will run properly. It is like the speed of the game, it won`t work or move if it 0.
We will create 2 functions Resume and Pause and change the timeScale value here.

    void Pause(){
        Time.timeScale = 0;
    }

    void Resume(){
        Time.timeScale = 1;
    }

And we will modify the visibility of the pause menu too. So the with the additional code it will be

    void Pause(){
        if(pauseCanvas != null){
            pauseCanvas.gameObject.SetActive(true);
            Time.timeScale = 0;
        }
    }

    public void Resume(){
        if(pauseCanvas != null){
            pauseCanvas.gameObject.SetActive(false);
            Time.timeScale = 1;
        }
    }

And we will disable the pause menu on start.

    private void Start() {
        if(pauseCanvas != null){
            pauseCanvas.gameObject.SetActive(false);
        }
    }

So this is the Final Script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour
    {
        #region Variables
        public Canvas pauseCanvas = null;
        #endregion
        private void Start() {
            if(pauseCanvas != null){
                pauseCanvas.gameObject.SetActive(false);
            }
        }

        void Pause(){
            if(pauseCanvas != null){
                pauseCanvas.gameObject.SetActive(true);
                Time.timeScale = 0;
            }
        }

        public void Resume(){
            if(pauseCanvas != null){
                pauseCanvas.gameObject.SetActive(false);
                Time.timeScale = 1;
            }
        }

        private void Update() {
            if(Input.GetKey("escape")){
                Pause();
            }
        }
    }



Now go into the unity editor and hit play.
Everything should work as expected.
Hit the Escape key to pause the game.