Can anyone tell me what's wrong with this countdown code in Unity 5?
I'm running this as a coroutine inside an OnTriggerStay if Collider other is a localplayer. It works fine except for the WaitforSeconds part. It doesn't seem to be yielding the correct amount of time for some reason. Do I need more yields or something? I want the 5 second countdown to only update every 1 second of course down to 1 or 0 but without a 5 inside the WaitForSeconds, the float timer reaches 0 in like 1 second instead of the 5 I want it to take. It also seems to be speeding up randomly as the value gets smaller when I exit and reenter the trigger multiple times. I'm lost haha. Maybe the CeilToInt is wrong too, I'm not used to math functions.
Code:
IEnumerator CountdownTimer()
{
Hud.Instance.respawnTimerText.text = "Respawning in " + Mathf.CeilToInt(countdownTimer) + "...";
while(countdownTimer > 0)
{
countdownTimer -= Time.deltaTime; //countdownTimer is a float set to 5f elsewhere
yield return new WaitForSeconds(5);
}
}
I'm running this as a coroutine inside an OnTriggerStay if Collider other is a localplayer. It works fine except for the WaitforSeconds part. It doesn't seem to be yielding the correct amount of time for some reason. Do I need more yields or something? I want the 5 second countdown to only update every 1 second of course down to 1 or 0 but without a 5 inside the WaitForSeconds, the float timer reaches 0 in like 1 second instead of the 5 I want it to take. It also seems to be speeding up randomly as the value gets smaller when I exit and reenter the trigger multiple times. I'm lost haha. Maybe the CeilToInt is wrong too, I'm not used to math functions.