Question by
estosan · Jul 02, 2021 at 10:21 AM ·
timer countdown
Turning Button into countdown timer
I am trying to turn a button into a countdown timer when it is pressed by having the text display the countdown. The timer only counts down when I press the associated button. Is what I want possible?
public void AbilityPress(int x)
{
//Temporary placeholders for ability cooldowns
float CurrentTime = 0f;
float StartingTime = 5f;
print("Ability " + x.ToString() + " pressed");
//Assigning Variables. In Order: Activated Button, Text of Button, Text of Button at time of activation
Button AbilityButton = GameObject.Find("AbilityOne").GetComponent<Button>(); ;
Text ability = GameObject.Find("AbilityOne").GetComponentInChildren<Text>();
string abilityname = ability.text;
print(ability.text);
//Setting up Cooldown Timer
//AbilityButton.interactable = false;
CurrentTime = StartingTime;
if(CurrentTime > 0)
{
CurrentTime -= 1 * Time.deltaTime;
ability.text = CurrentTime.ToString("0.00");
if(CurrentTime <= 0)
{
AbilityButton.interactable = true;
ability.text = abilityname;
}
}`
Comment
Your answer
Follow this Question
Related Questions
Making A Countdown Timer 0 Answers
How to make timer wait before resetting? 1 Answer
Countdown Timer 1 Answer
Is there a way to calculate elapsed Time in total? (Even when the App is closed etc.) 1 Answer
Display cooldown timer 1 Answer