Question by
Grimm_Reaper · Sep 08, 2017 at 03:02 PM ·
countdown
How do I make my coundown uniform?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public float displayTime = 3;
public Text timeText;
// Use this for initialization
void Start () {
timeText=GetComponent<Text>();
}
// Update is called once per frame
void Update () {
if(displayTime<=0)
{
displayTime = 3;
}
displayTime -= Time.deltaTime;
timeText.text = displayTime.ToString("f0");
}
}
I've typed this code. I feel it is because of the encountered "if" block. But i want to revert the timer back to 3 once it reaches 0. How do i do that without the loss in countdown accuracy?
Comment
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Timer : $$anonymous$$onoBehaviour
{
public int DisplayTime = 3;
public Text TimeText;
private void Start()
{
StartCoroutine(CountDown());
}
private void ResetCountDown()
{
DisplayTime = 3;
StartCoroutine(CountDown());
}
private IEnumerator CountDown()
{
while (DisplayTime >= 0)
{
yield return new WaitForSeconds(1.0f);
TimeText.text = DisplayTime.ToString();
DisplayTime--;
}
ResetCountDown();
}
}
Answer by Cuttlas-U · Sep 09, 2017 at 07:55 PM
hi; sorry i did not get u does this code works or not ? and what is your problem exactly ?
Answer by Grimm_Reaper · Sep 09, 2017 at 10:11 PM
I'm trying to count down from 3 to 0, then reset the timer to 3 then count down again...
What I am encountering is a non uniform count down.... What I mean is the time gap between 3 and 2 is different from that between 2 and 1.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134433im_/https://answers.unity.com/themes/thub/images/avi.jpg)