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;

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. https://aws1.discourse-cdn.com/business5/uploads/gamedev/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.