The Escape Key Is A Feature

Before we finish up our first build of our game and look at publishing. We need to think about how game’s are generally played, and how we can quit. On a PC, we tend to play most games in fullscreen. Therefore we don’t have an ‘X’ in the top right corner to easily exit.

Let’s add a simple function to exit the game when the Escape Key is pressed.

Our GameManager script is a suitable place for this. We want to be able to quit at any time so let’s use the Update method for this. Inside, let’s check for the Escape Key being pressed with an if check for GetKeyDown.

Within our If Statement, we want to access the Application and the modifier Quit.

Now it’s worth noting that we cannot close the Application when we are running our game in the Editor, so to test this, we’re going to need to build the game.

Or, for now, we can Debug.Log (“The Application is Quitting”) to save the hassle of creating a build to then find a bug!

Being able to Quit is such a simple but potentially overlooked feature. Remember to always add this functionality to your game!

A quick idea for an alternative is to have a Button call this function OnClick in our Main Menu, or why not both?



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