One key press returns multiple presses.

So I’m new to Unity and C# so maybe I’m doing something wrong but I don’t know. I followed and online tutorial and for my input manager my update method looks like this:

// Update is called once per frame void Update () { if (gm.State == GameState.Playing) { if (Input.GetKey("right")) { gm.Move(MoveDirection.Right); } else if (Input.GetKey("left")) { gm.Move(MoveDirection.Left); } else if (Input.GetKey("up")) { gm.Move(MoveDirection.Up); } else if (Input.GetKey("down")) { gm.Move(MoveDirection.Down); } } }

The problem is when I play the game and press any of those keys just once it returns like I pressed it multiple times even though I press it once and I am not holding it. gm is a reference to my game manager script that controls the moves. I can’t find anything on how to fix this but in the console it shows the three or four presses from my debug statements. Any help would be appreciated, thank you in advance.

GetKey returns the current state of the key every time you call it. Since you’re calling it at many frames per second - much faster than you can press and release it - it’s going to show as pressed for multiple frames in a row. You want the GetKeyDown method which will only return true once until the key is released and pressed again.

From the documentation:

Returns true during the frame the user
starts pressing down the key
identified by name.
…It will not return true until the
user has released the key and pressed
it again.

The documentation is a great source of help for this sort of thing.