How to quit a game in Unity

Every game needs to pause and quit. Unity has a simple one liner to make the game quit in any device. But you have to save your game before doing so. Efficient quitting will help player resume their game and keep the engagement rate higher. In this tutorial, you will learn how to quit your game in different devices and the different methods available in Unity.

Best practices to Quit the game

  1. Game should never quit directly from a game play session. If this option is available then there might be accidental quit during gameplay and will cause a bad experience for the player.
  2. Add a confirmation popup before you quit. You can also pause your game and add a quit button to the pause menu.
  3. Save player data and level data required to resume game before you quit.
  4. The quit button should be in a place where the player cannot press it accidentally.
Quit a game in Unity

Quit your game in Unity

It’s very simple to quit a game in Unity. You just need to call the method

Application.Quit();

let’s see how you can do this in different scenario

Using a Button to quit the game

Create a public function which calls “Application.Quit”.

using UnityEngine;

public class Exit_script : MonoBehaviour
{
    public void button_exit()
    {
    
        Application.Quit();

    }
}

Create a Button in Unity hierarchy window and name it as exit button. Add an on click event to the button.

Add the script above to an empty game object and drag and drop it to the button event. Select the public function you created in the list.

Add function to button

Using a Confirmation popup

By default, Unity has a popup for confirmation within the Unity editor. But, for builds you need to make your own panel.

Go to scene window and create a new panel by clicking Create>UI>Panel. Resize the panel to look like a popup. You can set the color based on your choice. Add to buttons to the panel for confirmation. Set the panel as deactivated in the scene.

Create a new button to activate the panel.

panel demo

Create an empty object and add the script below. Let’s call it Exit_manager.

using UnityEngine;

public class Exit_script : MonoBehaviour
{
    public GameObject confirmation_panel;
    public void button_exit()
    {
    
        Application.Quit();

    }
    public void enable_confirmation()
    {
        confirmation_panel.SetActive(true);
    }
    public void disable_confirmation()
    {
        confirmation_panel.SetActive(false);
    }
}

We have created three public functions. One to enable panel, one to disable the panel and one to quit the application.

game quit scene view

Add the respective function to the buttons.

1. “enable_confirmation” function to Exit button.

2. “disable_confirmation” function to No button.

3. “button_exit” function to Yes button.

Drag and drop the confirmation panel to the Exit_manager.

inspector view

Now you are all set. Once you press the button on the screen the confirmation popup will be displayed. if you press yes, the application will quit. If you press no the game will resume.

Enable confirmation with escape key

In case of windows, you can enable the exit pop when the escape key is pressed. To do that you can call the “enable_confirmation” function from an Update when the escape key is pressed.

This code will work with the back button on Android also.

using UnityEngine;

public class escape_button : MonoBehaviour
{
    Exit_script exi;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            exi.enable_confirmation();
        }
    }
}

Exit play mode in Unity editor

“Appication.Quit” does not work inside the Unity editor. It is designed to work only on game builds. So, if you want to quit the play mode when the exit button is pressed you need to add the namespace “using UnityEditor” and set the “EditorApplication.isPlaying” to false. Here is a code sample.

using UnityEngine;
using UnityEditor;

public class Exit_Editor : MonoBehaviour
{
    
    public void button_exit()
    {
    

        EditorApplication.isPlaying=false;

    }
}

If you want a common code for both editor and game builds then you can use Preprocessor directives to check whether the game is running on Unity Editor interface or on an actual device. Preprocessor directive give instructions to compiler to process the information before compiling the code.

using UnityEngine;
using UnityEditor;

public class Exit_Editor : MonoBehaviour
{
    
    public void button_exit()
    {
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #endif
        Application.Quit();

    }
}

How to quit a game with back button in Mobile devices

The earlier code using the escape button will work with back button of Android devices. Unity also has a simple one liner to quit the game when the back button is pressed.

Input.backButtonLeavesApp = true;

By default this value is set to false. You can set it to true in any script when the game starts. This function works on Android and Windows.

It will terminate the application on Android and suspend the application on Windows

You can check out our blog post on implementing back button in mobile devices for more details.

How to Cancel Game Quit in Unity

When Application is asked to Quit Unity event handler raises an event notify application quit. You can subscribe to this event and use it for other purposes like Auto saving a game or checking if the player actually wants to quit.

The method that we use to subscribe to the event needs to return a Boolean. If you return true the application will continue and quit. If you return false the application quit process will be cancelled.

Let’s dig into the code

using UnityEngine;

public class Cancel_QuitExample
{
    void Awake()
    {
        Application.wantsToQuit += StopQuit;//Subscribing to the wants to quit event
    }

    static bool StopQuit()
    {
        return false;//If you return True the application will Quit
    }  
    
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Level Designer’s Role in Game development How to Use Game Analytics? 10 Tips for Designing Multi-Platform Games Tips for creating Game Environments Game designer vs Game developer