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.

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.

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.