Creating a Quit Game Feature in Unity

Brady Sponholz
3 min readFeb 4, 2024

--

As you are others are playing your new game outside of the Unity editor, you will have notices there is no way to Quit your game without closing the application. Games in Unity do not automatically come with a quit game feature. We need to create this for the players.

In our GameManager script, we can simply add an IF statement that will quit the game if the Escape key is pressed.

Quitting a game this way could cause some frustrations for the player, so I will instead pause the game with Escape, and add UI that will ask then to confirm if they want to quit.

To start, I will first create new ExitGame_Text within my Canvas, as well as two buttons for Yes and No.

In our GameManager we can now create the desired behaviors. We need to add a serialized field for our ExitGame_Text and on start set the value to false.

In the void Update(), we want the Escape key to set the ExitGame_Text to true, and pause the game.

Now we need to create two public voids to set the behaviors for our Yes and No buttons. Yes will quit the game and no will set the ExitGame_Text to false and resume the game.

All that’s left is to put our ExitGame_Text into our new SerializeField and then set the behavior on click for the buttons. If you don’t remember how to do that, the steps are HERE. We now have a feature for allowing our player to pause and quit the game.

If you wanted, you could have also sent the player back to the start screen if they chose to quit, and then add an exit game button there. I will be removing that scene, so I won’t be doing that right now, but will in the future when I add new UI.

--

--

Brady Sponholz

Unity Developer | C# Software Engineer | Game Developer | Army Veteran