How to Pause and Resume game in Unity3D

How to create pause and resume button for our Unity games? It’s easy. In this video tutorial I will show you how to set up the Pause and Resume UI.

Here is the code used in this video (Create a new C# file and name it PauseResume, then paste this code):

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

public class PauseResume : MonoBehaviour
{

    public GameObject PauseScreen;
    public GameObject PauseButton;

    bool GamePaused;


    // Start is called before the first frame update
    void Start()
    {
        GamePaused = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (GamePaused)
            Time.timeScale = 0;
        else
            Time.timeScale = 1;
    }

    public void PauseGame()
    {
        GamePaused = true;
        PauseScreen.SetActive(true);
        PauseButton.SetActive(false);
    }

    public void ResumeGame()
    {
        GamePaused = false;
        PauseScreen.SetActive(false);
        PauseButton.SetActive(true);
    }
}

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *