Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[SOLVED] Input.GetKey not working (Unity 5.2)

Discussion in 'Scripting' started by Shushustorm, Sep 17, 2015.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    EDIT:
    Actually, I just found out this is a completely different issue. It's not related to preprocessors. They do work.
    What doesn't work anymore is getting input from the keyboard:
    Code (CSharp):
    1. if (Input.GetKeyDown("l")) {Debug.Log("Pressed L");}
    None of my Input.GetKey or Input.GetKeyDown are getting called.
    Touch input works still fine using UnityRemote 4.


    Hello everyone!
    Is this a bug or am I missing something?
    I tried using #if UNITY_EDITOR #endif for code that is for testing only.
    In C#, this works perfectly, but when doing this in Javascript, it won't be called.

    Documentation doesn't state anything specific concerning Javascript.
    ( http://docs.unity3d.com/Manual/PlatformDependentCompilation.html )

    Greetings,
    Shu
     
    Last edited: Sep 17, 2015
  2. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    545
    I've been using it myself, but as #ifdef UNITY_EDITOR #endif afaik. Maybe it's that difference? (I'm not too used to C preprocessor things)

    EDIT: what was that code tag doing there
     
    Last edited: Sep 18, 2015
    Shushustorm likes this.
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    I just found out this issue is related to Input rather than preprocessors, which I just edited in my original post. Thanks for your reply, though! I didn't know about #ifdef!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    out of curiosity if someone told you "my input doesn't work" and expected you to know what the problem was based purely on that, what would you tell them?
     
  5. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    I don't know. What additional information should I give you to elaborate the situation?
    When I put
    Code (CSharp):
    1. if (Input.GetKeyDown("l")) {Debug.Log("Pressed L");}

    (from edited original post)
    in Update() and press the L button, there won't be a message in the console.
    On the other hand, all the touch events in Update() work when using UnityRemote, though.
     
  6. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    545
    Try using the Input.GetKeyDown(KeyCode code) overload. The string overload probably parses it to the KeyCode enum, which defines L but not l (non-capital L) (maybe it's case sensitive, no idea).
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. Input.GetKey(KeyCode.L);
    2. Input.GetKeyDown(KeyCode.L);
    3. Input.GetKeyUp(KeyCode.L);
    I'm using also KeyCode. It's provided by unity so why not using it. also you can't make typo mistakes
     
  8. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    @Zuntatos
    @martinmr
    Thanks for your help! It works now. I already tried using KeyCode yesterday and it failed, too. Today, after changing, it work. This was kind of weird to me (because it wouldn't work yesterday), so I changed it back to the version without KeyCodes and it works as well.
    I don't really know what this problem was about. Maybe the editor or my machine just needed a restart for whatever reason. You're right about using KeyCodes, though. The non-KeyCode version sure is more prone to typos.
     
    lclemens likes this.
  9. ebubeud1996

    ebubeud1996

    Joined:
    Mar 31, 2018
    Posts:
    6

    Did you save before restarting? I'm facing a similar issue
     
  10. ebubeud1996

    ebubeud1996

    Joined:
    Mar 31, 2018
    Posts:
    6
    It works now. I restarted the application.Thanks
     
  11. lasdoo5

    lasdoo5

    Joined:
    May 18, 2018
    Posts:
    2
    i had the same issue.. every thing was fine. infect i didn't change anything. so i closed unity and re open. then it worked... :D
     
  12. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    114
    Yes, restarting Unity fixed the issue. Does UE4 have issues like this too?
     
  13. ben-rasooli

    ben-rasooli

    Joined:
    May 1, 2014
    Posts:
    31
    Also, if you are running the game through the Device Simulator, the keyboard input doesn't work there. So change to Game view
     
    kurtisaur likes this.
unityunity