Question by
cmonroy · May 16, 2016 at 01:05 PM ·
coroutinewaitforseconds
WaitForSeconds returns control to calling routine before it completes?
I want to randomize my Idle state using a coroutine before changing to another state. The code:
void EnemyIdle()
{
Debug.Log("EnemyState is: Idle");
enemyAgent.Stop();
StartCoroutine(RandomWaiter());
currentEnemyState = EnemyState.Think;
}
...
IEnumerator RandomWaiter()
{
float waitTime;
waitTime = Random.Range(1f, 20f);
print(waitTime);
yield return new WaitForSeconds(waitTime);
//print(Time.time);
}
It enters the coroutine -since it shows me how many seconds should be waiting- but it immediately returns to the calling routine without having waited for the random amount of time. The code, of course, does not show any errors but it neither pauses for the expected time.
Since the idea is to have each enemy to be idle for a random amount of time before moving to the next state, perhaps some of you folks could enlight me as to what I'm doing wrong.
Thanks!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612080914im_/https://answers.unity.com/themes/thub/images/avi.jpg)