• 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
1
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
2

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 · 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 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
0

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.

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

8 People are following this question.

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

Related Questions

Why does input seem to be handled differently in Game View vs an actual build? 0 Answers

Physics behaving differently when switching controls from Input.GetKeyDown to Input.GetAxis - C# 1 Answer

Input Manager problem setting with different GamePads 0 Answers

How to access input manager through a shortcut? 0 Answers

How can I assign a xbox joystick to one player, and an Oculus Touch joystick to another player? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges