Getting multiple user inputs eventhough im pressing each key once

Hi,

When i press arrow keys or enter once i get a random number of input counts all at once from each key instead of just one. I pressed each key only once. Code is exactly as described in the video. See below for output:

image

void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            min = guess;
            guess = (max + min) / 2;
            Debug.Log("Is it higher or lower than..." + guess);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            max = guess;
            guess = (max + min) / 2;
            Debug.Log("Is it higher or lower than..." + guess);
        }
        else if (Input.GetKey(KeyCode.Return))
        {
            Debug.Log("I am a genius!");
        }
    }

Using visual studio 2019 & unity 2019

Hi,

Could you please format your code with {…} and the tab key to increase its readability?


See also:

Done.

Thank you. I do not see any issue with your code. Is your script attached to multiple game objects?

Doesn’t look it. But i could be wrong. Please see below:

Could you also share a screenshot of your console? Is “Collapse” enabled? If so, try to disable it.

The screenshot from the console is in the first post. Collapse is off. If you see the first message we have 6 inputs for up arrow, 4 inputs for down arrow and 6 for return/enter even though each key was pressed just once. There is no consistency in the number of inputs but in each occasion there were more than just one.

I actually wanted to see the whole console, not just the messages in the console. Sometimes things are enabled, which makes it hard to interpret the messages.

I don’t know which message is for the up and down arrow respectively. The best would be to add a dinstinguishable Debug.Log to the first two blocks.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Try using Input.GetKeyDown, rather then Input.GetKey.

For anyone else who may have this issue the solution by David_Lamonaca appears to be the solution in this case.

As a new learner I thought it would be fun to try and recreate the OP’s original issue and the GetKey appears to register the key press multiple times (I.E not just when pushed down).

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms