# Keypress Left is called twice in Update when key is pressed only once

I have a piece of code that is changing the position of player when left key is pressed. It is inside of Update() function. I know, Update is called multiple times, but since I have an ifstatement to check if left arrow is pressed, it should update only once. I have tested using print statement that once pressed, it gets called twice.

Problem: Position updated twice when key is pressed only once.

Below given is the structure of my code:

void Update()
{
if (Input.GetKeyDown (KeyCode.LeftArrow))
{
print ("PRESSEEEEEEEEEEEEEEEEEEDDDDDDDDDDDDDD");
}
}


How can I fix this?

• Your question says the problem is "Position updated twice when key is pressed only once." However your code doesn't mention anything about updating any position, only printing a debug message. Was that a mistake, and you meant to say "the message is pressed twice" ? – jhocking Aug 24 '14 at 23:11
• @jhocking: I have just not posted the position updation logic since I am sure that it has no bugs that will make the script behave that way and the "pressed" was to cross check if I was right, and yes I was since it got printed twice on a single press – Simran kaur Aug 24 '14 at 23:13

I can think of two ways of solving this

1. Use LateUpdate function instead of Update

LateUpdate is said to be called just once per frame, so it should fix the problem. However, depending on your project, its side effect could be to change your train of thought when dealing with logic for the same execution order of scripts.

2. Use a flag

You could use a flag for a double check.

Example code:

using UnityEngine;
using System.Collections;

public class YourClass : MonoBehaviour
{
private bool isKeyPressed = false;

void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow) && !isKeyPressed)
{
isKeyPressed = true;
// TODO
// your logic here when button pressed
}

if (Input.GetKeyUp(KeyCode.LeftArrow) && isKeyPressed)
{
isKeyPressed = false;
// TODO
// your logic here when button released
}
}
}

• Did not work :/ and LateUpdate is not an option for me because of some code dependency – Simran kaur Aug 24 '14 at 22:54
• I initialize flag with true and check if that's true and end of the if, I set it to false. Now, after it's set to false, it does not move, when to set it back to true – Simran kaur Aug 24 '14 at 23:11
• This is just a workaround and doesn't address the source of the problem. – MichaelHouse Aug 24 '14 at 23:52

Double check that this script isn't in the scene multiple times, either attached to multiple objects or attached multiple times to the same object. That latter mistake trips me up once in a while.

• :I did, script is attached to a single object only that is my player – Simran kaur Aug 24 '14 at 23:14
• whelp that's literally the only thing I can think of to make the KeyDown condition fire twice with one keypress – jhocking Aug 24 '14 at 23:17
• Could you please comment on how I can solve this by using a flag? I basically did not understand when to set the flag back to teu if I set it to false at the end of loop as user pctroll suggested – Simran kaur Aug 24 '14 at 23:20
• This is most likely the problem. It's more likely you've missed a script instance. – MichaelHouse Aug 24 '14 at 23:38

I had the same script on 4 buttons and it was getting called 4 times as expected from reading this thread. Here is how I solved it. Each button has a name. Instead of checking for bool like the answer above, check for the name button. a simple print function reveled the name of the button and I did this.

void Update(){
if(Input.GetKeyDown(KeyCode.UpArrow) && gameObject.name == "down 1 force Button"){
print("I was touched: " + gameObject.name);
}
}