- Home /
Starting Coroutine within Start method
Hi All,
I use C# for scripting in Unity.
Say I have a coroutine method...let name ia f so it goes somthing like this:
private IEnumerator f()
{
while (true)
{
do things
}
}
Rather calling the f every frame the coroutine uses while(true) loop.
The Start method look like thaht:
void Start { startCoroutine(f());
}
When I press play button Unity freezes and does not responds. I check Unity's doc and it looks like it should work. I don't understands the problem. please help me
Thank u in advance
Answer by Joshua · Jul 12, 2011 at 05:01 AM
Unity doesn't freeze, it is just executing your while loop. It will keep doing this until the condition is no longer true, or you yield.
private IEnumerator f()
{
while (true)
{
//do things
yield return null; //after having done things, exit this loop and wait null frames before reentering. So the next frame 'things' will happen again.
}
}
Wow it works!!! I thought yield return null will exit the coroutine I guess it just hold the method for one frame. Thank u very much :)
It does exit the coroutine, and then re-enters after a specified amount of time/frames. Glad to have helped. :)