Simple GetKeyDown call not working?

I am trying to do a very simple keyboard input detection script. I want something to happen when I press the ‘D’ key but it is not detecting it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebuggerDisplay : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("D pressed");
        }
    }
}

When I press ‘D’ in game, no debugger message appears. I see the messages from other Update calls in my game. What am I doing wrong?? This is driving me crazy

Looks good to me. You sure you put it on an enabled GameObject in the scene you’re running?

Confirmed, your script works. Make sure it’s on an enabled GameObject still alive in your scene.

4614208--431680--Screen Shot 2019-06-04 at 3.45.15 PM.png

Ah, you were right. I never assigned the script to an active object so unity was never listening for the key press in the first place. Thanks!

3 Likes

I made this error first in 2013 and I’m still makin’ it today. :slight_smile:

2 Likes

Called it on the OP, because…

I do too, we all do. :wink:

It happens.

Eventually you’ll get the opposite problem where you have a similar script on more than one object. You really feel stupid when you spend an hour trying to figure out why Update is getting called twice per frame :stuck_out_tongue:

2 Likes