Hi,
I’m trying to make my first CoRoutine work. It seems to me that the code is identical to both the documentation and all the posts here on the forum, but I’m fairly sure the problem stems from some oversight on my part. I simply cannot spot it, though, and post in the hopes that someone can point it out to me.
I get no errors.
The code runs once, printing the time once, and then simply appears to stop. At least no subsequent prints appear.
The condition: if (count >= sceneSc.ballList.Count) is never true.
From an Update() function, I start the CoRoutine like this:
if (!waitingForBalls)
{
waitingForBalls = true;
StartCoroutine(WaitForBallsToStop());
}
This is the CoRoutine function:
private IEnumerator WaitForBallsToStop()
{
int count = 0;
foreach (GameObject go in sceneSc.ballList)
{
//Count the balls that have stopped
if(go.rigidbody.IsSleeping())
{
count++;
}
}
//if count is equal to total, change State.
if (count >= sceneSc.ballList.Count)
{
print("Sending ball stopped event");
EventManager.instance.QueueEvent(new BallStopped());
SetState(StateType.PLAYING, this);
GuiBuilder.infoText = "Ready for action!";
waitingForBalls = false;
StopCoroutine("WaitForBallsToStop");
}
//wait a bit and calculate again
print(Time.time);
yield return new WaitForSeconds(0.5f);
}
I’m fairly sure I could make this work with Invoke() or InvokeRepeating(), but I’m very annoyed that I don’t understand what’s going on here.
As an aside, are there inherent differences in the performance (as in, use of CPU), of InvokeRepeating() and a CoRoutine?