The very short Git Setup

by Lance Gold

Pressing the Escape button is helpful to exit a game, especially if the game uses up the whole device screen

Return to index

(a) Open GameManager’s script and locate the Update() method.

(b) Add check for Input.GetKeyDown().


using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
...
    void Update()
    {
...
        // if Escape key pressed, then quit application
        if( Input.GetKeyDown(KeyCode.Escape))
        {

        }
    }
}

Application.Quit

Declaration: public static void Quit();

Parameters: exitCodeAn optional exit code to return when the player application terminates on Windows, macOS, and Linux. Defaults to 0.

Description: Quits the player application.

Shut down the running application. The Application.Quit call is ignored in the Editor.

From the Unity — Scripting API

Here is the documentation sample code:


using UnityEngine;
using System.Collections;

// Quits the player when the user hits escape

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey("escape"))
        {
            Application.Quit();
        }
    }
}

Here is the helpful dialogue before “Application.Quit()”:

“Are you sure you want to quite? Quit / Cancel” while a timer counts down several seconds.