- Home /
How do I close my game using the escape key?
Hi,
So I am new to making games and I am struggling to add a code which will close my game down when I press the escape key. So far I have this code:
target.addEventListener(Input.GetKeyDown(KeyCode.escape));
{
if (Input.GetKeyDown(KeyCode.escape)){
Application.Exit();
}
}
This is more than likely wrong in so many ways so I would appreciate some help
Thanks.
Answer by Matthew012 · Oct 22, 2013 at 08:34 PM
I just tested this and it worked fine for me.
function Update () {
if(Input.GetKeyDown("escape")) {//When a key is pressed down it see if it was the escape key if it was it will execute the code
Application.Quit(); // Quits the game
}
}
Remember that this will only work when you Build & Run your application it will not work in the unity editor.
Answer by Deathdefy · Oct 22, 2013 at 08:12 PM
Hey RAWR, here is a solid example of how to close your application. One important thing you need to know is that Application.Quit() does not apply while in the editor. It only works properly after your application is built.
http://docs.unity3d.com/Documentation/ScriptReference/Application.Quit.html
Your answer
Follow this Question
Related Questions
Error UCE0001 ";"Expected 2 Answers
Game Pad Joystick not working! 2 Answers
Bolt Flowchart is not detecting Script on GameObject 0 Answers
unity not working help 2 Answers
Problem with calculating distance 1 Answer