how to pause game by script in unity ( with android back button or with computer esc key )

In this post we will create a simple ui button to pause game and show paused UI and other button to resume game so let's get started.
  • Create a Button UI gameObject and named as pause button. (you can name whatever you want).change its text component to pause.
  • if you want to show a (  ||  )  icon to pause game then delete text child object of pause button and then drag your icon in source image of Image component in inspection window.
  • Then create a Panel UI GameObject named as pause menu UI and create a child text ui gameObject (write paused it it text value and place it at top of pause menu panel) and one child button UI gameObject placed in the middle of pause menu panel and named as resume(Change button to resume). Make sure both text and button are child of pause menu UI.
  • this is a simplest UI. You can design your own according to your need. You can add many UI buttons , UI Image, and text in pause menu ui panel and design in your own way like replay, get free coins, home and much more.
  • lets create a C# script and named as pause_game. and declare a pauseUI gameObject.
  • Create an other empty gameObject and named as pause menu script and drag and drop pause_game script in it.



C# script code

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

public class pause_game : MonoBehaviour
{
    [SerializeFieldprivate GameObject pause_UI;
    bool is_paused=false;
    bool change_pause_status=true;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if(Input.GetKeyDown(KeyCode.Escape))
        {
            is_paused = !is_paused;
            change_pause_status=true;
        }


        if(change_pause_status)
        {
            if(is_paused)
            {
                Time.timeScale=0;
                pause_UI.SetActive(true);
                change_pause_status=false;
            }
            else
            {
                Time.timeScale=1;
                pause_UI.SetActive(false);
                change_pause_status=false;
            }
        }
        
    }

    public void Pause_btn_clicked_fn()
    {
        is_paused=true;
        change_pause_status=true;
    }

    public void resume_btn_click_fn()
    {
        is_paused=false;
        change_pause_status=true;
    }


}


C# script Code explanation

1. declaration of variables

    [SerializeFieldprivate GameObject pause_UI;
    bool is_paused=false;
    bool change_pause_status=true;

  • first we have declared a private GameObject pause_UI. You have noticed that we have used [SerializeField]  in very biginning so that pause you can show in inspector window. It is used to show private variables in inspector window without declaring them public.
  • in second, we have declare a bool named as is_paused and initialize it as false this variable hold value of are current game status it is at a time paused or running. if game paused then is_paused is true and otherwise false.
  • next is change_pause_status of bool type. this addition variable we have created so that we don't need to run is_paused in each update method call. It will be turn true when we change from pause to resume or resume to pause. It is initialize as true so that it will deactivate pause menu ui if we forget to deactivate

2.  functions for resuming and pausing game

    public void Pause_btn_clicked_fn()
    {
        is_paused=true;
        change_pause_status=true;
    }

    public void resume_btn_click_fn()
    {
        is_paused=false;
        change_pause_status=true;
    }

  • first we have create a function to pause game. In this function we set is_paused to true and change_puase_status to true. change_pause_status is set true so code inside in if() statement will run and is_paused is true so it pause game.
  •  we create a other function to resume game and so in it we set is_paused to false and change_pause_status to true.

3.  Adding both functions in onClick component of pause and resume button


  • to add a functionality with button click. Select pause button and then in OnClick component click on ( + ) button then drag and drop pause menu script gameObject in it and select function then pause_game and pause_btn_clicked_fn(). so now when we click on this button it will run pause_btn_clicked_fn().
  • same  steps you can repeat for resume button and add resume_btn_click_fn() in it. 

.   4. code for back button or computer esc key

        if(Input.GetKeyDown(KeyCode.Escape))
        {
            is_paused = !is_paused;
            change_pause_status=true;
        }

  • here we have used GetKeyDown event in an if statement so that whenever we press escape or mobile back button code in if statement will run. KeyCode.Escape will work same as smart phone back button.
  • inside if statement we use  is_paused = !is_paused  so it assign value of opposite value of is_paused means if is_paused is false then !is_false will be true and this value now assigned with is_paused.
  • change_pause_status set to true so code for pause or resume will run.       

5. Showing and hiding pause menu code in update method

       if(change_pause_status)
        {
            if(is_paused)
            {
                Time.timeScale=0;
                pause_UI.SetActive(true);
                change_pause_status=false;
            }
            else
            {
                Time.timeScale=1;
                pause_UI.SetActive(false);
                change_pause_status=false;
            }
        }

  • All code inside if statement only run when change_pause_status is true. We use this condition so that this code do not run at each loop and we made change_pause_status true when we change game to pause or resume.
  • next we use an if - else statement and use condition is_paused = true so that if is_paused is true then code inside if statement will run other or if is false then else statement will run.
  • in if statement we make timeScale to zero (0) so all time (or Physics) based will stop. for making pause menu visible we SetActive function to true to activate pause_UI gameObject. and in last we set change_pause_status to false so that this if state will not run until next event.
  • same is done in else statement when is_paused is false but set opposite value. first we set timeScale to 1. and deactivate pause_UI by passing false in SetActive function. in last we set change_pause_status to false so it will not run again.

I have you can understand how we can pause and resume game. You can design pause menu in your ways and add whatever you want.

Popular posts from this blog

Positioning GameObjects in unity editor

Creating count down timer in unity by c# script

Changing Image in unity by C# script