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. Maybe 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 Key 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...

2 Answers

2

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).

if (Input.GetKey(KeyCode.Space)) { timeStarted = true; introductionText.text = ""; } I do this and it doesn't want to work, it doesn't matter which keycode I use

@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!