Unity3D: How to Quit Application

Gabriel Perez
2 min readApr 22, 2021

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!

--

--

Gabriel Perez

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