Unity3D: How to Quit Application

So your game needs an Escape button? I’ll show you how in this short article!

If you don’t have a GameManager script, I would suggest creating one to handle the logic for loading scenes and quitting the application.

I attached the GameManager script to the Game_Manager game object.

We can now open the script to start coding. Clean up your script if needed. I am starting fresh for this example. All we need is an Update() function to have our escape key working.

using UnityEngine;public class GameManager : MonoBehaviour
{
private void Update()
{
}
}

We need to let Unity know when we hit the Escape key, then exit the application.

In the Update() function, we check if we have pressed down the Escape key.

using UnityEngine;public class GameManager : MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(Keycode.Escape))
{
}
}
}

And when we do, we need to access the Application that deals with run-time data and call its Quit() function.

using UnityEngine;public class GameManager : MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(Keycode.Escape))
{
Application.Quit();
}
}
}

You can encapsulate the Application.Quit() into a public function and call it by using a UI Button OnClick() event.

I have it so that when I press the Escape key, the Pause Panel shows up instead of quitting the application. A UI button calls the Quit() function to quit the application.

As you can see, it’s a simple implementation. It can be used in a UI Button or not! I hope this has shed some light!

Thank you for your time!

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store