It’s not very magical. A WaitForSeconds object is a simple timer. The time measured does not reset without any specific reason to - beacause that would be pretty magical. As a result, the object will never wait again.
You can, however, use a custom WaitForSeconds object that has a reset function. I’ve written one for you:
using UnityEngine;
public class WaitTimer : CustomYieldInstruction
{
private float timeLeft;
private float lastTime;
public override bool keepWaiting
{
get
{
timeLeft -= Time.deltaTime;
return timeLeft > 0;
}
}
public WaitTimer(float time)
{
Reset(time);
}
public void Reset(float time = 0)
{
if(time == 0)
{
timeLeft = lastTime;
}
else
{
lastTime = timeLeft = time;
}
}
}
You need to reset the timer before using it again:
var timer = new WaitTimer(3);
yield return timer;
Debug.Log("3");
timer.Reset();
yield return timer;
Debug.Log("6");
timer.Reset(4);
yield return timer;
Debug.Log("10");
If you like inplace methods, you could add a function in addition to Reset that returns the timer:
public WaitTimer Again(float time = 0)
{
Reset(time);
return this;
}
and then use it like this:
var timer = new WaitTimer(3);
yield return timer;
Debug.Log("3");
yield return timer.Again();
Debug.Log("6");