Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Input.GetKeyDown not registering 100% of the time

Discussion in 'Scripting' started by james432213, Sep 6, 2018.

  1. james432213

    james432213

    Joined:
    Jun 14, 2018
    Posts:
    10
    The desired output will present 80% of the time, other times I will have to press the b key several more times to get a single Debug.Log message to print. It's like it gets "stuck" thus doesn't register the key being pressed down.

    Thank you.


    void Update()
    {
    if (Input.GetKeyDown("b"))
    {
    Debug.Log("b key was pressed");
    }
    }

    EDIT: The problem seems to occur only in this project. I tried the same code in another and it works fine. Any ideas? So, far I've identified that it occurs when dropping items from the screen are destroyed.
    I found the fix: It's because the if (Input.GetKeyDown("b")) code was in the item class, so obviously when the item gets destroyed the Input would stop working.
     
    Last edited: Sep 6, 2018
  2. DecapitatedRain

    DecapitatedRain

    Joined:
    Dec 15, 2016
    Posts:
    5
    This may seem simple, but make make sure you have log messages enable by clicking the little (!) Otherwise, make sure you don't have too many other Debug.Log messages that may be blocking the function from working properly. I had one that said the xyz coords every frame. Removed that, got it working again.
     
  3. james432213

    james432213

    Joined:
    Jun 14, 2018
    Posts:
    10
    I just used "Debug.Log("b key was pressed");" as an example. If I insert a function I still get the problem, the function not registering 100% of the time.
    I have disabled all other Debug.Log messages but it hasn't fixed the problem.

    ty
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,915
    GetKeyDown only registers the first frame the key gets pressed. You have to lift the finger in order for GetKeyDown to register a second time. Are you aware of that? For continuous press use GetKey().

    As for logging, if you have the „collate identical messages“ feature enabled disable it. Otherwise you get the same log but you will hardly see it because there‘s just a gray bubble to the right of the log message whose number increases. It doesn‘t even sort the newest logs to the bottom, hence you might miss it (and that‘s why I never use that feature).
     
    Ryiah likes this.
  5. james432213

    james432213

    Joined:
    Jun 14, 2018
    Posts:
    10
    Yes, I'm aware of these differences between GetKeyDown() and GetKey(). I desire GetKeyDown(), but the problem is that If I tap the b key 10 times only 8 Debug.Log messages/8 code blocks will be executed. I will need to spam the key to make the key execute the code again.

    The problem seems to occur only in this project. I tried the same code in another and it works fine. Any ideas?

    EDIT: I found the fix: It's because the if (Input.GetKeyDown("b")) code was in the item class, so obviously when the item gets destroyed the Input would stop working.
     
    Last edited: Sep 6, 2018
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,915
    Is the framerate of your app very low? In that case you may be pressing faster than the update rate.