The Escape Button is a Feature

Credits: Dribble — Alfrey Davilla

In your unity project, nothing exists until you’ve created it. Not the lives or score system, not the player movement or input systems. Everything the developer has to create. And today we’ll be creating a function that quits the game when the player presses the “ESC” key.

There are a few ways you can map input in your scripts. you can use something in the lines of “Input.GetKeyDown(KeyCode.Escape)”. This is a way to “hard-code” an input and makes it harder to make changes to keybindings later on. For a small game like ours we might not even need to allow the user to create different keybindings but for the sake of consistency and to allow more customization later on we’ll be using “Input.GetButton(“ButtonNameHere”)”. Using this input system require a bit more setting up, but once it’s done, you don’t need to worry about going back into code to change keybindings.

Configuring our Keybindings

In your open Unity project, head to “Edit” then to “Project Settings” and click on the “Input Manager” tab. By default, Unity will provide you with 18 buttons to use. I’ve already used the “Horizontal”, “Vertical” and “Fire1” buttons in my project. You’ll see there’s a button called “Cancel” and decided to use that as our escape key. I’ve replaced the default “Positive” field with “escape”, but please note unity is very picky with key naming and if you try to use “Escape” with a capital “E” it will not save. For your reference, I’ve added the list of keys here.

As you can see the button name is “Cancel”, please take note of the name and its capitalization because Unity is very specific when it comes to naming and referencing things.

What will ESC do?

The big question that may have risen is “what is the purpose of the ESC key?”. The answer is that when the user wants to quit their game (assuming it’s a standalone build) then they have to press the ESC key. Although this functionality is very basic for now, later we could create a menu where the user can pause the game to take a break or restart the current level or even make changes to settings.

Before we cover the code we need to understand another important part of the functionality, and that is how to actually close the game. Our project, whether it’s a game or software is considered an “Application” and to close the application we use a Unity built-in function called “Application.Quit();”. We have to be careful when using this because if you create this functionality with the intention of a standalone build, but then upload your game to a WebGL platform will cause your game to freeze in the browser. We could create a serialized bool and call it “_isWebGLBuild” and run a check to see if the bool is false before the code is executed. But I won’t be covering that today.

The Code

In a moment you’ll see how easy it is to create the function to close the game. Since our game has a “GameManager” script and object that already has some input functionality to restart the game, we’ll be adding to the “Update” function.


As you can see that implementing an ESC to quit function was very easy. And now you can make changes to the inputs later on without having to revisit code and making changes there.



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