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 we 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. Even better, if you can add the quit popup as a 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

Its very simple to quit a game in Unity. You just need to call the method

Application.Quit();

lets 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 an 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;

    }
}

Leave a Reply

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