3
\$\begingroup\$

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?

\$\endgroup\$
  • 3
    \$\begingroup\$ 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" ? \$\endgroup\$ – jhocking Aug 24 '14 at 23:11
  • \$\begingroup\$ @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 \$\endgroup\$ – Simran kaur Aug 24 '14 at 23:13
2
\$\begingroup\$

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
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Did not work :/ and LateUpdate is not an option for me because of some code dependency \$\endgroup\$ – Simran kaur Aug 24 '14 at 22:54
  • \$\begingroup\$ 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 \$\endgroup\$ – Simran kaur Aug 24 '14 at 23:11
  • 1
    \$\begingroup\$ This is just a workaround and doesn't address the source of the problem. \$\endgroup\$ – MichaelHouse Aug 24 '14 at 23:52
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ :I did, script is attached to a single object only that is my player \$\endgroup\$ – Simran kaur Aug 24 '14 at 23:14
  • \$\begingroup\$ whelp that's literally the only thing I can think of to make the KeyDown condition fire twice with one keypress \$\endgroup\$ – jhocking Aug 24 '14 at 23:17
  • \$\begingroup\$ 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 \$\endgroup\$ – Simran kaur Aug 24 '14 at 23:20
  • 3
    \$\begingroup\$ This is most likely the problem. It's more likely you've missed a script instance. \$\endgroup\$ – MichaelHouse Aug 24 '14 at 23:38
-1
\$\begingroup\$

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);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.