- Home /
yield works, but yield WaitForSeconds does not?
I trigger the following UnityScript function once when my game pauses (timeScale zero).
function animatePie( pieStart : float, pieEnd : float ) {
for (var f = pieStart; f >= pieEnd; f -= .0333) {
upgradePie.GetComponent(UnityEngine.UI.Image).fillAmount = f;
Debug.Log("TEST");
yield WaitForSeconds(.0333);
}
}
But for some reason it runs only once: "TEST" is only output a single time. It doesn't seem to matter what value I put in WaitForSeconds, the loop never cycles.
However, if I take out the WaitForSeconds and use yield alone, all is well: "TEST" outputs multiple times (and the fillAmount animates nicely).
But yield alone doesn't give me control over the speed of the animation, so it will vary per device. (Nor can I handle that with Time.deltaTime, since the game is paused.)
Why would yield fail to execute when I use WaitForSeconds?
Thanks for any help recognizing my mental block!
Answer by Eric5h5 · May 06, 2015 at 03:16 AM
WaitForSeconds is scaled by Time.timeScale, so it won't work if you set the timeScale to 0, which I assume is what you mean when you say "the game is paused".
Thanks! That's what I would have first guessed, but I happened to run into bad info on here somewhere, saying that WaitForSeconds was independent of timeScale.
I'll create my own deltaTime using the system clock. I assume that must be the preferred method for animating a menu while the game action is paused. (Would be neat if there was something like "Time.HiddenDeltaTime" to access the delta that still goes on while frame Updates are called during timeScale 0.)
@$$anonymous$$organ:
You mean something like Time.unscaledDeltaTime?
Excellent, yes!
Weird--I've found so many discussions and solutions for this situation, but never a mention of the most direct approach!
It wasn't added until 4.5, so none of the older discussions would include it.
Your answer
Follow this Question
Related Questions
Waypoint / Yield help 1 Answer
Stuck while solving a Coroutine problem 0 Answers
Wait For Seconds to Load level C# 2 Answers