Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
2
Question by adriailla · Dec 24, 2016 at 07:39 PM · input manager

Input.GetKeyDown not working

Hi, I'm trying to get an input from the "space" button, but it looks like it doesn't take the input when I click. Here's my code:

 else if (other.gameObject.tag == "RedDoor") {
             if (redKey_canvas.enabled) {
                    print("entered the door with the key");
                 if (Input.GetKeyDown(KeyCode.Space)) {
                     print("Space clicked!);
                 
                 //OPEN DOOR
                 other.gameObject.GetComponent<SpriteRenderer>().sprite = enterDoor;
                 redKey_canvas.enabled = false;
                 }
             }

In the console, I get the "entered door with key" message, but when I press Space, nothing happens.

I've also tried to add an Axis to the Input Manager named "DoorEnter", but it doesn't work either...

Comment
Add comment · Show 1
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 Xib · Dec 24, 2016 at 09:39 PM 0
Share

Are you calling this in an update function? If yes in which one? Because Input doesn't get reliably registered in FixedUpdate for example. You notice some key events aren't registered at all.

Also untill which point do these debug messages get printed? Do you really have an object with the tag "RedDoor"? $$anonymous$$aybe you're checking the tag on the collider which might have a different tag than it's parent if you put the collider in a child object.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Balgy · Dec 24, 2016 at 11:39 PM

Input.GetKeyDown needs to be called from the Update() function since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

If you already have it in the Update() function, then try

 Input.GetKeyDown("space")

Instead of

 Input.GetKeyDown(KeyCode.Space)

seems to have solved my problem.

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 Burkard · Sep 12, 2021 at 01:18 AM 0
Share

Surprisingly, five years after, this is still the thing to be done - at least when building for mobile... so what's the point of having KeyCode?

avatar image
1

Answer by tanoshimi · Dec 24, 2016 at 07:40 PM

What function is this code contained in? I'm guessing it's something like OnTriggerEnter? That fires in a single frame in which you enter a trigger. And Input.GetKeyDown also fires in only a single frame in which you press a key. So, do you really press the space bar key and enter the trigger at exactly the same 1/60th of a second?

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
avatar image
1

Answer by knightOfSecrets · Aug 21, 2019 at 01:19 PM

make sure that after running the game at unity u click by the mouse on the game scene (i.e. make sure the game scene is selected)

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 roshanjafri31_unity · Mar 08, 2020 at 02:46 PM 0
Share

Thank you this worked . lol sometimes why you have the scene and game view side by side , issues like this pop up.

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Input Manager equal to all platforms? 0 Answers

Is joystick axis inertia possible in Input Manager? 0 Answers

GearVR Unity 5.5 Touchpad Input Button Press Inconsistent? 0 Answers

New input system local multiplayer: move the second player 2 Answers

New input system bind meta quest 2 controller 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