Making a fill take exactly n seconds to complete

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?

I think your mistake is putting the yield statement outside of the loop

Inside of your while loop place a:

yield return new WaitForSeconds(n);

this will wait n seconds (n being a float that can be less than 1) before the loop resume again. Since you seem to be filling with detlaTime you should be able to use

yield return new WaitForSeconds(deltaTime)