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

Posted by Simran kaur on Game Development See other posts from Game Development or by Simran kaur
Published on 2014-08-24T22:11:09Z Indexed on 2014/08/24 22:35 UTC
Read the original article Hit count: 255

Filed under:

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");
    }
}

I looked up on web and what was suggested id this:

if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.LeftArrow)
{
    print("pressed");
}

But, It gives me an error that says:

Object reference not set to instance of an object

How can I fix this?

© Game Development or respective owner

Related posts about unity