If and Else statements both activate from "if (Input.GetKeyDown(KeyCode.key))"

Hello there :slight_smile:

I got a rather long code containing some “if (Input.GetKeyDown(KeyCode.[KEY]))” functions.

If those keys are pressed there are two possible results, but no matter what I do it seems like both my If result and my else result is registrered.

I’ve tried to search online and it seems like “if (Input.GetKeyDown(KeyCode.[KEY]))” run twice during an updateframe, meaning that it will activate both my If and my Else statement.

Is there a way to prevent “if (Input.GetKeyDown(KeyCode.[KEY]))” from running twice during an updateframe?

Can you provide the script? Use code tags so will be easier to identify the problem

1 Like

I don’t know if the code below makes any sense, taken out of context, but it is part of a much longer - and with 99% certainty very inefficiently written - script.

The idea is that two players battle in a quiz game.
If you make a correct answer you get 2 points.
if you make a correct answer, but does so after the other player also chose the correct answer, you get 1 point.
If you make a wrong answer you get -1 point.

The -1 point part work.
The 2nd to answer correctly also work.
But the first one to answer correctly seem to get points for answering both first and second, meaning that they currently get 3 points instead of 2 and I can’t figure out how to fix it.

I should note that I’m very new to this and I’m almost certain that I’ve done a lot of very weird choices during the script, making it even more difficult to correct - and also to help, I’m aware :-/

//This is one of the hotkey assigners

        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            if (player2Cooldown == false)
            {
                player2Pressed1 = true;
                option1Button.onClick.Invoke();
                player2Answered.SetActive(true);
                player2Cooldown = true;
            }
        }


//This is one of the outcomes. 

   public void Option1Selected(bool isCorrect)
    {
        if (isCorrect)
        {
            if (player2Pressed1 == true)
            { 
                if (player1Pressed1 == true)
                { //This if statement is meant to activate, if player 1 has already given the same answer beforehand.
                    player2Score += currentRoundData.pointsAddedForSecondCorrectAnswer;
                }
                else
                {
                    player2Score += currentRoundData.pointsAddedForFirstCorrectAnswer;
                }
            }

            if (player1Pressed1 == true)
            {
                if (player2Pressed1 == true)
                {
                    player1Score += currentRoundData.pointsAddedForSecondCorrectAnswer;
                }

                else
                {
                    player1Score += currentRoundData.pointsAddedForFirstCorrectAnswer;
                }
            }
        }

        else
        {
            if (player2Pressed1 == true)
            {
                player2Score -= currentRoundData.pointsSustractedForWrongAnswer;
            }

            if (player1Pressed1 == true)
            {
                player1Score -= currentRoundData.pointsSustractedForWrongAnswer;
            }
        }
    }

Ok so when you’re handling input, You should make sure both player 1 and player 2 have answered before running the OptionSelectedFunction

What i think it’s doing is running on both clicks thats why its adding both points,

You could have an if statement like

if (Player1HasAnswered  == true && Player2HasAnswered == true)
{
OptionSelected()
}

Ah yes of course! You’re right!
I’ll try to tinker with some solutions and then probably return if I can’t figure it out!

Thanks mate!