c# – Unity Coroutine unexpected behaivour
I am starting a coroutine which iterates a loop number of times and waiting a certain time between each iteration. But complation of the loop takes way more longer than the expected. Here is the code:
private IEnumerator CO_AddBladesInTime(int count, Blade blade)
{
var _spawnPos = blade.transform.position;
var _counter = 0;
var _firstTime = Time.time;
var _calculatedTime = 0f;
Debug.Log("entered coroutine at " + _firstTime);
while (_counter < count)
{
_counter++;
// AddNewBlade(_spawnPos, blade.Level);
var _waitTime = bladeSpawnFreq / count;
_calculatedTime += _waitTime;
yield return new WaitForSeconds(_waitTime);
}
var _secondTime = Time.time;
Debug.Log("exited coroutine at " + _secondTime);
Debug.Log($"waited {_secondTime - _firstTime}");
Debug.Log($"expected wait time {_calculatedTime}");
}
Here is the results:
Am I missing something?
Read more here: Source link

