1

How do I avoid my UI buttons to run functions twice if clicked (accidentally) twice or more in a short time?

I have a UI button that calls a StartGame function in my game script. I attached the script to the button in the inspector. The function sets a few variables in order for the game to start.

If I accidentally click the button twice really quickly, the start function is executed twice and thus it screws up my variables. How do I avoid that?

I am thinking of splitting up the function so that it checks if it's already started, but that seems like a bit complex. Isn't there a standard setting in Unity UI or some standard code I missed? Tried to google but not much luck.

Simplified example of my code:

public void StartGame() {

MenuAnimator.SetBool ("Startbutton", false); // animates my start game panel off the screen.
lives = lives - 1;
Do Rest of Code();
}

In this case, clicking the UI menu Start Game button multiple times causes two or more lives to be deducted.

1
  • You can set up a flag (bool) and reset it after X seconds. If you need further details, please post your button code on your question. Commented Aug 14, 2016 at 1:36

3 Answers 3

3

There's no need for a lock, just have a boolean that flips each click and the button only triggers a response when the bool is true.

first click -> true -> response.

second click -> false -> return.

Sign up to request clarification or add additional context in comments.

Comments

1

What about something like this? This will prevent double clicks that happen within 1 second (or however many you want)

bool buttonLocked;
System.Timers.Timer t = new System.Timers.Timer(1000); //however many milliseconds
t.Elapsed += new EventHandler(resetFlag);

private void button_clicked(object sender, EventArgs e){
    if(!buttonLocked){
       // Handle Click
       buttonLocked= true;
       t.Enabled = true;
    }
}

private void resetFlag(){
    buttonLocked = false;
    t.Enabled = false;
}

Or you could probably just use whether or not the timer is enabled as a flag assuming the timer is properly initialized. I like having a separate flag though for readability.

Full disclosure- I'm at a bar typing this on my phone so it's untested but I think that should work

1 Comment

Lol @ answering questions on StackOverflow in a bar!
0

Due the fact that I canno't comment, here is my answer. You can lock the variable.

First create a lock

private readonly object _locker = new object();

In your lock property set/get

get { lock (_locker) { return this.myVar; } }
set { lock (_locker) { this.myVar = value; } }

Now you can lock this MyVar variable using

public void MethodA()
{
    lock(_locker)
    {
        if(myVar == 1)
          myVar = 0;
    }
}

This is mostly used with multi-threading but It should do the job for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.