Coding Question, in Text101, why are we using Input.GetKeyDown ()?

This is a question about why we are structuring our program this way. (getting back into programming from decades away, apologizes if this is simplistic)

It seems to me that our using a stack of If, else if statements requires that our method be completed while the key is being held down. I suppose that isn’t an unreasonable assumption but is discomforting and in big methods and rapid gameplay it may be false.

I would prefer to assign to a variable the value of the keypress. I can’t find a method that dose this, is there one? Input.GetKeyDown () returns a boolean.


You could iterate through every KeyCode if you want to store which key has been pressed as a variable, I would suggest though that this would be fairly overkill.

In this example game you are only giving the player a few simple options to choose from, thus, you are only interested in a few keys and you already know which ones because you told them :slight_smile:

The check is taking place with the Update() method which fires once per frame, so currently it is more efficient to check for just a few KeyCodes than iterating all of them to decide which was pressed to then run through the same logic to act upon the key pressed.

Hope this helps.

1 Like

Thanks, it certainly is more efficient. I suppose if we use a string then we must assume that only one key is pressed. Is a string only passed if enter/ return key is pressed?

Thanks for that link, that is more what i was thinking but still a bit past my programming.

No problem. :slight_smile:

You can pass a string in rather than a keycode, so, “space” for example, personally I would use the key codes.

See also;

Privacy & Terms