How to Quit The Game In Unity: Proper Ways to Close a Game
Often when you’re developing a game and you’re having fun coding all the cool stuff for your game, and you forget that your users need to be able to leave the game. Everyone has to take a break at some point, and you will need a way to do that for your users.
To programmatically quit the game made with Unity, call the Quit method of Application class, i.e. Application.Quit. This will immediately tell the game to quit. Some platforms, however, require a different implementation to make the game exit properly.
Do note that quitting the game using the Application.Quit method will not work when it’s running inside the Play Mode in Unity Editor. Build and run the game first if you want to test your game’s quitting functionality.
I will go into details on how to deal with various aspects concerning quitting the game below so make sure to keep reading.
How to Quit The Game In Unity
Telling your game to close or quit is a very simple task. All you have to do is call the Application.Quit() method and the game will shut itself down immediately.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour
{
void Update()
{
if (Input.GetKey(KeyCode.X))
{
Application.Quit();
}
}
}
Code language: C# (cs)
The example script above will make the game close when the “X” key is pressed. Just make sure that the script is properly attached to a game object in the scene that is running.
Important: The Application.Quit() will be ignored while your game is running in Play Mode inside Unity Editor. To properly test its functionality, you need to build your game first and then try it on the built version of the game.
How to Build Your Game To Test Quitting Functionality
As stated previously, the Application.Quit() method will not work if your game is running in Play Mode inside Unity Editor. You have to build your game and run it outside of Unity Editor.
To create a game build, navigate to File > Build Settings… or press Ctrl+Shift+B to open the Build Settings window.
Add the scenes you want, or click on the Add Open Scenes button to add all currently open scenes to the build.
Select the platform you want to test on and click on the Build And Run button. You’ll be prompt to pick where the game files will be saved to.
Once you’ve selected where you’ll save your built to, wait for a bit while Unity is building the game. The game will start once the build process is finished, then you can proceed to test your game. Any subsequent builds after this will take much less time to build.
You can also go to File > Build And Run or press Ctrl+B to build your game right away without selecting a platform.
How to Quit The Game via An In-Game Button
Instead of pressing a button on the keyboard to quit the game, a lot of times you will want the quit button as an in-game button instead to make it easier for the player.
Binding a method of a script to a button in the game is not hard to do. Here, let’s try it right now.
First, let’s create a button and put it in the UI. You can add a button to the scene by right-clicking on the hierarchy window, or clicking on the plus sign on the top-left corner, and selecting UI > Button.
This will add a button to the UI canvas. Rename it to whatever you want.
You can change the button’s label by selecting the Text object under the button object and editing the text in the inspector.
If your game is a 3D game, you might want to toggle the 2D mode on when adjusting the positions of the UI elements.
Adjust the position of the quit button to where you want it to be.
Now, we need a script to hold our method to quit the game.
Let’s create a script and put it somewhere. Right-click on the Project window and select Create > C# Script, or click on the plus sign at the top-left corner of the Project window and select C# Script.
Name it whatever you like and then double-click on it to open the code editor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour
{
public void QuitGame()
{
Application.Quit();
}
}
Code language: C# (cs)
The script above will tell the game to quit when the QuitGame() method is called.
But how will the method be called from a button? There’s a way to do it. Let’s go back to the button object be created earlier.
Add the script we just created to the button object. You can do this by dragging the script file and dropping it onto the button object, or you can add it via the Add Component button in the inspector.
You should be able to see the script in the inspector once the script has been successfully attached to the button.
Take a look in the inspector, you should be able to see a component called “Button” inside the button object similar to the image above. The “On Click ()” section is where you will be adding a method to trigger when the button is clicked.
Click on the plus sign at the bottom to add an empty On Click event to the list.
Drag the script component that’s attached to the button into the little box that says “None (Object)” to expose the Button component to various methods available inside.
Then, click on the dropdown box that says “No Function” and select [Your Script’s Class Name] > [Public Method You Want To Call. In this case, my script’s name is Manager and the method is called QuitGame() so it will be Manager > QuitGame ().
The result will look something like the image below.
And you’re done! Build the game and try clicking on the quit button. It should quit the game as intended.
Technically, you can add any game object to the On Click () section. It will grab all of the available public methods of all the components within that object and let you select them. So, potentially, you could have the script attached to a different game object and make the call from there, too.
Note that this method doesn’t work while the game is running inside the Play Mode in Unity Editor. And you can’t close games made for iOS as they’re not designed to be closed. For more information about closing an application programmatically in iOS, I suggest taking a look here.
And that’s it. That’s how you give your game the ability to quit or close itself whenever you want!