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.