Hello everyone.
I remember reading somewhere that objects declared for suspending coroutines such as WaitForSeconds, WaitForSecondsRealTime should be declared once before a loop and the declared yield object should be used in following iterations. I remember it was about getting rid of unnecessary GC allocations.
But, here is a weird behaviour as far as I can grasp things
private IEnumerator SomeCoroutine()
{
float yieldReturnInterval = 0.1f;
WaitForSecondsRealtime waitForSecondsRealTime = new WaitForSecondsRealtime(yieldReturnInterval);
// Do some stuff here.
while (remainingDuration > 0)
{
remainingDuration -= yieldReturnInterval;
yield return waitForSecondsRealTime;
}
// Do some other stuff here.
SomeFancyFunction();
}
First one above is what I understand from “declare once and use it as much as you can” advice.
But loging the time in console, I can clearly see that remaining time reaches 0 much faster than intended time.
Code below works fine, but I have to create a new WaitForSecondsRealTime object per iteration of while loop. So, what do you think the problem could be here? (Or where did I misunderstand things )
private IEnumerator SomeCoroutine()
{
float yieldReturnInterval = 0.1f;
// Do some stuff here.
while (remainingDuration > 0)
{
remainingDuration -= yieldReturnInterval;
yield return new WaitForSecondsRealTime(yieldReturnInterval);
}
// Do some other stuff here.
SomeFancyFunction();
}