I’m working on a timer class I can call for a whole bunch of different purposes, and as part of it I’m trying to write a function that will gradually draw a sprite with a radial fill such as to indicate the timer’s progress- when the sprite is invisible, it isn’t running, when it’s completely drawn the timer has completed. Currently I’m running the timer in one coroutine, and the fill in another:
IEnumerator CountDown(float time){
countdownRunning = true;
StartCoroutine("CountDownAnimation",time);
yield return new WaitForSeconds(time);
Debug.Log ("Ding ding ding");
if(countdownRunning)
//do stuff
countdownRunning = false;
}
IEnumerator CountDownAnimation(float time){
float animationTime = time;
while (animationTime > 0) {
animationTime -= Time.deltaTime;
countdownSprite.fillAmount = animationTime/time;
}
yield return null;
}
The obvious problem is that since coroutines don’t run once per frame, animationTime continually returns true and the sprite jumps instantly from 0% fill to 100% fill. Is there a very simple way that I’m missing to correctly set the fill each frame, given that the waitforseconds() on CountDown precludes setting it in the same loop that runs the timer?