Creating a Quit Game Button

Michael Desena
3 min readJun 30, 2023

--

Closing an application in Unity is extremely easy. You can close an application through code or assign a UI button to quit the game.

Using a UI Button:

In a canvas create a UI button, in our case, we used a button gameObject with a child Text Mesh Pro text element.

Creating a button in the canvas

The button component is extremely handy in expanding UI and UX. You are able to assign custom code logic while still being able to adjust visuals to child gameObjects. Customize the text component to your liking.

Button GameObject and Component
Text component child to button gameObject.

Create a script to manage UI and assign it to a gameObject in the hierarchy. Within the script create a public method. This method will be assigned to the UI button created earlier. Unity’s Application class allows users to obtain data from the application and also control what the user can do once the game is built. The code “Application.Quit()” allows us to quit the game but only after the game is built, it will not work in the Unity editor.

UI manager-script.
Code to close the application.

Assign this method to the button component by dragging and dropping the UI manager gameObject into the OnClick section of the component. Navigate to the QuitGame() method. Your button will not be able to quit the game!

Assigning QuitGame() to button.

Through Code:

Quitting the game through code is as simple as placing the Application.Quit() logic when the game is intended to close.

Example code.
Quit game button functionality.

--

--