Button Cooldown
How can I prevent a player from spam clicking a button to glitch the map/level? im looking for a cooldown-ish script, that makes it so you can't press a certain button before 5 seconds has passed.
Answer by $$anonymous$$ · Sep 26, 2015 at 10:53 PM
Do these steps in your script which is attached to your button.
Declare a bool type variable called cooldown in your outer scope of your script.
private bool cooldown = false;
Then when your button gets clicked, which can only be clicked if your cooldown variable is set to false, you start an Invoke method, to Invoke your ResetCooldown method after 5.0f seconds, then set your cooldown variable to true, so it can't be clicked again until it's set to false again.
void OnMouseDown() {
if ( cooldown == false ) {
//Do something
Invoke("ResetCooldown",5.0f);
cooldown = true;
}
}
Your ResetCooldown method sets your cooldown variable back to false, so your button can be clicked again.
void ResetCooldown(){
cooldown = false;
}
For more information about Invoke method please check out this link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Your answer
Follow this Question
Related Questions
Unity Android build multiplying decimal numbers by 10 for an unknown reason 0 Answers
Doodle Jump Game Platform Spawn Issue Unity2D 0 Answers
I can't figure out how to do this 2 Answers
Moving left and right with one button in 2d Game 1 Answer
Camera Movement Question - How to follow player in Y axis only above certain threshold? 0 Answers