- Home /
How do i add a delay to how often a unity ui button may be used?
Hello everyone! I am currently having an issue which i have no idea how to solve so hopefully someone out there might be able to help me!
The issue I am having is that I am unsure how i could add a delay to how often a button may be used, i have tried using yield return new WaitForSeconds however this does not seem to add a delay
Any suggestions on how to fix this or a workaround to this will be greatly appreciated!
Answer by ShadyProductions · Jul 19, 2018 at 07:53 PM
You can do this by messing with the Time.deltaTime that unity provides.
Here is an example:
private float timeLeft = 30.0f; //30secs
public bool CanButtonBePressed = true;
void Update()
{
if (!CanButtonBePressed)
{
timeLeft -= Time.deltaTime;
if(timeLeft <= 0)
{
CanButtonBePressed = true;
timeLeft = 30.0f; //reset
}
}
}
Just make sure to set canButtonBePressed to false when u press the button.
Wow!! Thank you so much, I changed your code slightly and it worked without any issues!
Your answer
Follow this Question
Related Questions
check alpha of gameObject C# 3 Answers
Putting a delay in a button OnClick ? (Javascript) 3 Answers
How to get exact button input 4 Answers
Make delay for spawn 3 Answers
How to make UI buttons so my player can move along certain lines 0 Answers