Tutorial: a pause menu

Escape stops the game and shows a panel with three buttons: carry on, start again, or leave.

1. Build the panel

Build the panel
  1. Add a UI panel named Pause Panel.
  2. Add three buttons in it. In the Inspector set their action to resume, restart and quit.

2. The script

The script

Scripts/PauseMenu.cs

public class PauseMenu : Behaviour
{
    bool paused;

    public override void Start() { Set(false); }

    public override void Update(float deltaTime)
    {
        if (Input.GetKeyDown("Escape")) Set(!paused);
        if (!paused) return;
        // (a button reports the frame it was clicked, by the name of its action)
        if (UI.Clicked("resume")) Set(false);
        if (UI.Clicked("restart")) { Set(false); Scene.Reload(); }
        if (UI.Clicked("quit")) Application.Quit();
    }

    void Set(bool on)
    {
        paused = on;
        // time stands still while the panel is up: physics, animations and Update's deltaTime all stop
        Time.timeScale = on ? 0f : 1f;
        UI.SetVisible("Pause Panel", on);
        Cursor.visible = on;
    }
}

Save it as Scripts/PauseMenu.cs and add it to any object.

3. How it works

How it works

4. Try it

Try it

Press PLAY, then Escape. The panel appears and the game freezes; resume continues from the same moment.

Next: save, load and a high score.