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.
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: Unity - Scripting API: MonoBehaviour.Invoke
havent got to test it out yet, AS im on vacation, but it definetly looks like it works thanks a lot! @z0hann