- Home /
The question is answered, right answer was accepted
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
Answer by jashan · Aug 09, 2010 at 02:41 PM
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").
Since $$anonymous$$ 2 wasn't seen for over a year (and the question is already 5 years old.....), i've accepted the answer.
Internet points are always good even if they're late. ;)
Answer by tcarausu · Mar 05, 2017 at 03:25 PM
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.
Answer by Regah · Aug 16, 2017 at 12:22 AM
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 :( do any of you have any solutions?