- Home /
How do I get Input.GetKeyDown(KeyCode.Escape) to register?
I am trying to make my game exit back to the main menu when I hit the escape key or the back button on my android.
I am currently using Input.GetKeyDown(KeyCode.Escape) to check if the key has been hit before attempting to switch scenes. I have the code in my MainGameScript's Update, right above the code where I check if the mouse button has been pressed. The game registers the mouse button being pressed just fine, but will not register the escape key at all.
I have set breakpoints inside of both if statements and the breakpoint inside of the Escape if check never gets triggered, but the Mouse Button breakpoint gets triggered every time.
Does anyone know what I'm doing wrong here?
Here is the code I am using:
void Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
AudioSource.PlayClipAtPoint(quit, new Vector3());
Application.Quit();
}
if(Input.GetButtonDown ("Fire1"))
{
Ray ray = gameCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(ray, out hit))
{
if(!playGame.Clicked && playGame.ColliderTransform == hit.transform)
{
playGame.Click();
AudioSource.PlayClipAtPoint(start, new Vector3());
StartCoroutine(CallAnim());
Application.LoadLevel("Desktop");
}
if(!exitGame.Clicked && exitGame.ColliderTransform == hit.transform)
{
exitGame.Click();
AudioSource.PlayClipAtPoint(quit, new Vector3());
StartCoroutine(CallAnim());
Application.Quit();
}
}
}
}
Everything inside of the if(Input.GetButtonDown ("Fire1"))
works fine, but the if(Input.GetKeyDown(KeyCode.Escape))
isn't being hit at all
Can't say I've had a problem with it. If you post your code, it might turn out that there's some other problem.
Curious. Glad to see your problem is fixed, but from where I'm sitting that fix doesn't make sense. Are you sure that quit
wasn't null, or anything like that? An exception from playing a null clip would stop the rest of the code from executing.
Answer by yogee · Oct 22, 2013 at 04:18 AM
use GetKey() not GetKeyDown()
Thank you, Get$$anonymous$$ey() worked. What exactly is wrong with using Get$$anonymous$$eyDown() though? Shouldn't that trigger as soon as the key is pressed?
Answer by davididev · Jul 31, 2014 at 09:57 PM
I find that on older phones (on Gingerbread), GetKey() doesn't work either- however, Input.GetAxis() does (once I define an axis that uses menu or escape)