Button requires to be pressed twice on first load to register

I have a script which controls player movement and all works fine except for my dash mechanic. When the script first loads the first input of the “Dash” button does nothing but then works perfectly after that, it is as if though the first button press is not being registered. I will add the affected code below. Thank you.

if(direction == 0)
        {
            if (Input.GetButtonDown("Dodge") && !dodged && (grounded || airDodgeObtained))
            {
                if(moveX < 0)
                {
                    direction = 1;
                }
                else if (moveX > 0)
                {
                    direction = 2;
                }
            }
        }
        else
        {
            if(dodgeTime <= 0)
            {
                direction = 0;
                dodgeTime = startDodgeTime;
                playerRB.velocity = Vector2.zero;
            }
            else
            {
                dodgeTime -= Time.deltaTime;

                if (direction == 1)
                {
                    playerRB.velocity = Vector2.left * dodgeSpeed;
                    dodged = true;
                    dodgeCooldown = startDodgeCooldown;

                    if (dodgeJumpUnlocked)
                    {
                        canDoubleJump = true;
                    }
                }
                else if (direction == 2)
                {
                    playerRB.velocity = Vector2.right * dodgeSpeed;
                    dodged = true;
                    dodgeCooldown = startDodgeCooldown;

                    if (dodgeJumpUnlocked)
                    {
                        canDoubleJump = true;
                    }
                }
            }
        }

        if (dodged)
        {
            dodgeCooldown -= Time.deltaTime;

            if(dodgeCooldown <= 0)
            {
                dodged = false;
            }
        }

Hi @Diluision88 ,

This can be happening for example if your app does not have the focus when you try to click the button for the first time.

If not, please provide more information about the concrete situation in where you’re having the issue, like if it’s happening on the Game view in the Editor or in a build.

Good luck with it!

Hi, the issue occurs both in the ‘game’ view as well as in a build. All other button presses occur correctly from start apart from pressing my “dodge” button which takes a double press the first time to work, then after the initial first “null” press it works perfectly.

Nevermind I figured it out, I wasn’t setting : dodgeTime = startDodgeTime in start function so it required a button press to set its value before allowing the script to continue. Thanks anyway

1 Like