- Home /
Input.GetKey not working, even in Update()
Today when I went to use the script that controls movement I could not get Input.GetKey nor Input.GetKeyDown to work at all. Even simplifying the script to this, when the scene runs (with a gameobject with the script attached in it of course) the only one of the two debug logs below that runs is the else statement one. No matter how much I mash the d key nothing happens. I also tried various other key codes including other letters, numbers, shifts, all to no avail. When I searched for the problem I saw that this could happen if the code was in in Update, but even below you can see that it is. Anybody have an idea why Unity can't catch any keys?
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
void Update()
{
if (Input.GetKey(KeyCode.D))
{
Debug.Log("Detected the key");
}
else
{
Debug.Log("Went into else");
}
}
}
Is the script attached to any game object? I know you say it is. $$anonymous$$aybe add a Start
function that has a Debug.Log()
so you know the script is executed.
The script is definitely running and attached to an object as I do get the "Went into else" message every frame.
Have you tried it with a different $$anonymous$$ey than D?
Yeah, I tried a few other letters, a number, and left shift all with no luck.
If you remove the Debug in the else scope? Because maybe you just miss the message...
Answer by Bijak · Jan 08, 2015 at 12:35 AM
It turned out that I had just forgot to initialize a variable somewhere else in another script, causing a statement to fire every frame that reloaded the scene. I assume that this is what was consuming the events (Or at least fixing it fixed this).
I have no clue what I did, but re-writing the same thing worked for me, although copying yours didn't.
Edit: I noticed with yours, the problem didn't actually come to the console as an error, but a warning. Looking at the script on my object in the inspector said it might be a compiler error.
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour
{
void Update ()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
Debug.Log("Works");
}
else
{
Debug.Log("Doesn't Work");
}
}
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
timeStarted = true;
introductionText.text = "";
}
I do this and it doesn't want to work, it doesn't matter which keycode I use
Answer by TommyTheProgrammer · Dec 28, 2018 at 02:48 AM
@Bijak Well, maybe you didn't turn on the "Message" in the console window. Press the little exclamation mark to enable messages. If you didn't try this, I'm SURE it WOULD WORK!
Your answer
![](https://koobas.hobune.stream/wayback/20220613175835im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Limiting and Rotating a Platform. 1 Answer
GetKey/GetButton not working if 2 Adjacent keys are being pressed at the same time 1 Answer
Is there a way to make a pc build that will not lose focus when selecting another app ? 1 Answer
Movement stops working randomly 1 Answer
Input.GetKeyUp for Input.GetAxisRaw 1 Answer