• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taira · Oct 22, 2013 at 03:40 AM · c#androidpc

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Oct 22, 2013 at 03:43 AM 0
Share

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.

avatar image rutter · Oct 22, 2013 at 04:48 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by yogee · Oct 22, 2013 at 04:18 AM

use GetKey() not GetKeyDown()

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taira · Oct 22, 2013 at 04:22 AM 1
Share

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?

avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I use WebClient to download files on Android when behind a proxy? 1 Answer

Soft turn with joystick 0 Answers

Jittery movement of sprite on device 1 Answer

How to make a menu like ironpants 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges