My "if else" condition doesn't works, I can still press all of them same time and the message's showing up too

image

Hi Beqa,

You wonā€™t be pressing all of them within the same frame though. The Input.GetKeyDown method only returns true during the frame that it is pressed down.

Also, it is better to copy/paste your code into your posts and apply the code formatting characters, rather than add screenshots. They are often quite different to read, especially on mobile devices without a lot of zooming/scrolling. It also prevents those that offer to help you from copy/pasting a part of your code back to you with suggestions/corrections.


See also;

1 Like

Hey Rob, in the lesson, about between 5:10 - 5:15 Rick says that after adding the ā€œif elseā€ conditions, when we press the keys same time, output is only one, I rewatched it with subtitles on. Mine prints both when I pressing two keys same time.

Iā€™m sorry about the screenshot instead of copy/paste. I didnā€™t think about someone copy/pasting it(And Iā€™m very used to expressing myself with screenshots, coding society is new to meEmoji ). As im not at my working pc now, Ill copy the code later if you wish.

The code is working perfectly for me. Could you post a video or something similar?

Iā€™ve noticed the same thing. I added in the ā€˜elseā€™ keywords, and still every time I hit two keys at the same time, they would both show up in the output. I kept at it for a while, though, and I finally did manage to get just one key to show up. Conclusion: It only SEEMS that you hit both keys at the same time, but in reality thereā€™s a tiny difference of at least one frame between them. Iā€™m not going to worry about it for now.

Thank you

I encounter the same problem. I think the logic here is that, you need to code it specifically saying that if detected 2 input at the same time, nothing happen, or even error message should pop up. /uploads/db2322/original/3X/a/d/ad9e6a8d81d9fdf92d3c7d344e5f9b693e8a55b6.mp4

Iā€™m also getting multiple keypresses, even though I know my code is correct.

I checked my FPS (frames per second) using the Stats tab on the Game window, and my program is running at over 2000 FPS. Every now and then I am successful at pressing two keys within that 1/2000 of a second window (along with whatever constraints my Logitech keyboard and Windows 10 are introducing), and only one of the keypress counters will increase. Most often, both go up at the same time.

I donā€™t think this is a code issue; I think itā€™s a speed issue.

Same thing here
i press 2 buttons and they both work and are shown in the console collapse menu

`here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

    Debug.Log("Welcome to Number Wizard, Yo!");
    Debug.Log("Pick a number");
    Debug.Log("The highest number is: " + max);
    Debug.Log("The lowest number is: " + min);
    Debug.Log("Thell me if your number is lower than 500");
    Debug.Log("Push Up = Higher, Push Down = Lower, Push Enter = Correct");


}

// Update is called once per frame
void Update()
{
    //Detect when the up arrow key is pressed down
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        // execute this code block
        Debug.Log("Up Arrow key was pressed.");
    }
    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        // execute this code block
        Debug.Log("Down Arrow key was pressed.");
    }
    else if (Input.GetKeyDown(KeyCode.Return))
    {
        //execute this code  block
        Debug.Log("Enter key was pressed.");
    }
}

}

In fact, if you go on the Game Tab and select VSync ON (middle dropdown menu) to throttle your FPS, the issue of simultaneous button pressing is less pronounced than if you do not have the ā€œelse ifā€ statements in the code.

Beqa,

Since your game is running at roughly 60 frames per second, it would be very difficult for you hit two keys at the same time in the SAME FRAME. This is likely why it appears to be not working for you.

Dale.

Privacy & Terms