How can I connect button to function?

Hello, we have a dash button in the game. If we press the button dash button is working well but the problem is if we press anywhere on screen it is working too! We only want to work that function with button.
Anyone help us please?

Code:

public void Dash()
    {
        if (Input.GetMouseButtonDown(0))

        {
            activeMoveSpeed = dashSpeed;
            dashCounter = dashLength;
        }
        if (dashCounter > 0)
        {
            canShoot = false;
            dashCounter -= Time.deltaTime;
            if (dashCounter <= 0)
            {
                canShoot = false;
                activeMoveSpeed = speed;
                dashCoolCounter = dashCoolDown;
            }
        }
        if (dashCoolCounter > 0)
        {
            canShoot = false;
            dashCoolCounter -= Time.deltaTime;
        }
    }
private void FixedUpdate()
    {
        Movement();
        Dash();
    }


6871187--801893--Screen Shot 2021-02-24 at 12.43.59.png

If you have the Dash Function attached to your Button you do not need it in the Update function. If it’s in Update it gets called every frame. Make sure that the behaviour you need from the button and the behaviour you need every frame are seperated from each other.

1 Like

Thanks for your reply, If I remove form update function, our button is not working:(

Step Debug through your function.
Quick idea: Remove the if (Input.GetMouseButtonDown(0)) → If you are toggling from the button you dont need to check wether the mouse is pressed anymore.

1 Like