- Home /
I have a button with a cooldown. How do I make it so the cooldown time increases with each press of the button?
Here is my code:
{
public GameObject Button;
public void Button()
{
///stuff here...///
}
Button.SetActive(false);
Invoke("ButtonCooldown",0.8f);
}
}
}
void ButtonCooldown()
{
Button.SetActive(true);
}
}
Basically, I want it to be that every time you press the button, the time you have to wait before pressing it again increases by say, 0.2f. Right now it has a cooldown of 0.8f every time you press it.
How can I do this?
Answer by David_Rios · Jul 27, 2018 at 02:00 AM
Hey Geoffrey, what you can do is keep the button cooldown value in a float variable and then add 0.2f to it every time your button has been cooled down.
For example, your modified code would be (untested):
public GameObject Button;
private float cooldownValue = 0.8f;
public void Button()
{
///stuff here...///
}
Button.SetActive(false);
Invoke("ButtonCooldown", cooldownValue);
}
}
}
void ButtonCooldown()
{
Button.SetActive(true);
//Adds 0.2f to the current value of the button's cooldown value
cooldownValue += 0.2f;
}
}
Works great. Thanks!
Could I also reduce the CooldownValue in Update by -0.1f or something to that effect to make it more dynamic?
Do you mean reduce it by 0.1f every time the button has cooled down? Because if you just put cooldownValue -= 0.1f
in the Update() method it would lead to it going infinitely negative.
Please clarify what you want to do :P
I just mean so that the added cooldown time isn't permenant. So like it would slowly go down while also still adding to it, so that it doesn't become like 10 seconds long.
Answer by Carterryan1990 · Jul 27, 2018 at 02:01 AM
public float timeIncrease ;//enter your increase float here;
public float invokeTime = 0.8f;//Invoke Time
//Each time this method is called invoke time will increase
public void button(){
Invoke("ButtonCoolDown", invokeTime);
invokeTime += timeIncrease;
}
The issue I see with this code snippet is that the cooldown time would increase before the button is set active again. The invoke time would be invokeTime
+ timeIncrease
the first time around. Just move the invokeTime += timeIncrease
line to past the Invoke function and it should be correct.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Script Two Button Functions At Once 1 Answer
Invoke on Struct or some else time wait 1 Answer
Multiple Cars not working 1 Answer
check alpha of gameObject C# 3 Answers