how to have the escape key actualy close the game build

Hi

I feel oh so incredibly stupid to have to ask this, so please don't remind me!

I've made a little game that I want to be able to send off etc, but when I run my builds (finished non-debug exe files) when I press the escape key, nothing happens. What do I need to add to the file to have it exit on the escape key, or do anything on the escape key (so I can have it go to a menu, where pressing exit will close the game)

Thanks

You need to click the checkmark to accept the answer.

3 Answers

3

What you need:

So, what you'd probably end up is a script which implements that following Update()-method (and is attached to some game object that's available all the time):

public void Update() {
    if (Input.GetKeyDown(KeyCode.Escape)) {
        Application.Quit();
    }
}

The JavaScript version of the same can be found in the documentation of Application.Quit (see above). It's using Input.GetKey instead of Input.GetKeyDown and also uses the version which takes the name of the key instead of the keycode (I guess that's kind of the JavaScript way of doing things ... put a typo in "escape" and it'll break and you'll spend hours debugging ... well, maybe you'll get a nice runtime error and debugging will only take a few moments ;-)

If this is the answer you were looking for, you should use the "check"-icon which you find below the up/down arrows to vote the answer up/down. That will mark the question as "answered" which will make it disappear from the list of unanswered questions which helps keep Unity Answers a useful place (it's kind of annoying to run through all the "unanswered questions" all the time just to find out that they're actually answered properly already because the person asking the question didn't follow their duty of also marking the correct answer "correct answer").

Nice, getting us noobs started :)

Since Jay 2 wasn't seen for over a year (and the question is already 5 years old.....), i've accepted the answer.

Awesome! Thanks!

Thank you so much. This helped me out a ton.

for some reason it doesn’t work i have tried key/keydown/ keycode with and without " " and no succes maybe i do something wrong?

Hey tcarausu, I am sure you already got your answer but I think its because it doesn't apply when you are in the editor. You have to build your game and run it from an executable for this to apply.

i tried the same, but my game has a first person camera which when i press esc it makes my mouse visible back again taking me out of game mode, so the esc thing does not work :frowning: do any of you have any solutions?