hello, I have a complicated issue. I have a script that runs a coroutine that has a WaitForSeconds in it. when the player steps on a button, the script plays an animation, waits for five seconds, then plays another animation. the problem is, I want to be able to delay the second animation by stepping on another button. the issue is that even if I add to the amount that the WaitForSeconds is supposed to wait for, if the WaitForSeconds amount started waiting already, it will use the value it started with even if the amount it’s supposed to wait for changes while it is waiting. is there a way to make the WaitForSeconds delay longer even after it has already started waiting?
What I would do @WerewoofPrime is:
Option #1
- When the coroutine gets created store
it into a variable.- Have a variable that stores the amount of time that has passed since
the coroutine started - When the other button is pressed stop the previous coroutine and start
a new one with a different
WaitForSeconds time based upon the
time that has passed of the previous
coroutine.
- Have a variable that stores the amount of time that has passed since
Option #2:
- Don’t work with coroutines but with
the update in your game.- Have a time it starts from and count it down
- When the player steps on the other button ad a fixed amount to the time
variable that gets counted down.
Hope these options help you and good luck.
private float coroutineResumeTime;
private IEnumerator MyCoroutine()
{
// …
coroutineResumeTime = Time.time + 5f;
yield return new WaitUntil(() => Time.time > coroutineResumeTime);
// ....
}
public void OnButtonPressed()
{
coroutineResumeTime += 10;
}
PS: Next time, please provide your code.
I just wouldn’t use WaitForSeconds here. You can manage the waiting yourself…
float waitDuration = 10f;
// instead of yield return new WaitForSeconds(waitDuration);
IEnumerator CR()
{
float elapsed = 0f;
while (elapsed < waitDuration)
{
elapsed += Time.deltaTime;
yield return null;
}
}
Now you can change the wait time while waiting, just by changing the value of waitDuration