GetKeyDown and GetKey not working? (2017.4.2f2)

Is there something in a project that can interfere with Input.GetKey and/or GetKeyDown from working?
Neither are currently working for a project I’m working with (I’m admittedly rather new to Unity).

Here’s the code I have in an active object:

// Update is called once per frame
	void Update () {
        Debug.Log("Im here");
        if (Input.GetKeyDown("space")) {
            Debug.Log("Im in");
            Instantiate(respawnPrefab, spawnPlace.transform.position, spawnPlace.transform.rotation);
        }

    }

When running, the log shows a steady stream of “Im here” messages, but no matter how much the space bar is pressed, “Im in” is never reported in the log and the prefab I’m trying to Instantiate never appears. (I’ve tried this with both GetKeyDown and GetKey).

Any suggestions?
Thanks in advance.
Have fun.

2 Answers

2

Good day.

Its highly recommended, when using GetKeyDown, GetMouse, and these things, to use the KeyCode instead of the string name of the key. I mean do this:

if (Input.GetKeyDown(KeyCode.Space)) 
if (Input.GetKeyDown(KeyCode.LeftAlt)) 
if (Input.GetKeyDown(KeyCode.Comma)) 

this will prevent you to make typing errors, and will be easy to find the correct name of each key (if your scripting program give you recommendations while typing)

Bye! :smiley:

Thank you! That worked Thanks again!

Thank you! That worked

Thanks again!