(question) is my code correct? i can still press two at the same time

{
// Start is called before the first frame update
void Start()
{
int max = 1000;
int min = 1;

    Debug.Log("Welcome to the Game");
    Debug.Log("Please pick a number");
    Debug.Log("The highest number you can choose is: " + max);
    Debug.Log("The lowest number you can choose is: " + min);
    Debug.Log("Tell me if your guess is higher or lower than 500");
    Debug.Log("push up if it is higher, press down if lower, push enter if correct");
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        Debug.Log("Up arrow key was pressed");
    }
    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        Debug.Log("Down arrow key was pressed");
    }
    else if (Input.GetKeyDown(KeyCode.Return))
    {
        Debug.Log("Enter key was pressed");
    }
}

}

Hi @Space_Cowboy,

Welcome to our community! :slight_smile:

Please add Time.frameCount to your Debug.Log method calls. Here is an example: Debug.Log(Time.frameCount + “---- Up Arrow key was pressed.”);

Update gets called once per frame.

Test your game again. What is the output in your console?


See also:

I assume this is suppose to show frame rate but where would I see that?

As far as his question Space_Cowboy the code looks correct but the frame rates are probably off. If there is a slight delay when pushing both buttons it will show it as both buttons being pushed. This is what I found to happen when I first put the code in and realized it after playing around with it.

Am I correct on all of this Nina?

Not the framerate. Just the frame number.

If you get two different frame numbers, the code was obviously not executed at the same time but in two different frames, which means that Update was called twice. Depending on the frame rate, you will have to press your keys really fast to get only one message in your console.

When you have a framerate of 60, you would have to press your keys within less than 1/60 second to see only one message. It is likely that you have a much higher framerate, which means that you’ll have to click even faster.

Got it, so it shows which frame your in when you push the button within your frame rate. Again where do I see this number at?

The frame number will be shown at the beginning of the line that is being sent to the console. So for example if you press the up arrow, the console will display something like:

26---- Up Arrow key was pressed.

Regarding the key press being repeated multiple times, the best option to prevent this, I would suggest, is to use Input.GetKeyUp - honestly I was a little surprised that the tutorial used key down - common practice is to use key up, in part to prevent this issue.

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

Privacy & Terms