How to Pause a Game in Unity – Unity By Example

How to Pause a Game in Unity


In most games, it is desirable to interrupt the game at some point in order to do something else, such as taking a break or changing options.

Steps

  • Create script Pause.cs
using UnityEngine;

public class Pause : MonoBehaviour
{
    [SerializeField] private KeyCode _key = KeyCode.Escape;
    private bool _isPause;

    private void Update()
    {
        if (Input.GetKeyDown(_key))
        {
            Time.timeScale = _isPause ? 1.0f : 0.0f;
            _isPause = !_isPause;
        }
    }
}
  • Create canvas in it create empty object add text.
  • Add script Pause.cs to Pause obejct.
  • Select key to pause, default key is escape.

Result

Download

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments