- Home /
Problem with while loop in Coroutine
Hey Unity Community.
I hope all is well.
I am having a brain fart and can not seam to be able to figure out why this simple test would not work.
void Start(){
StartCoroutine("whileTest");
}
IEnumerator whileTest()
{
while(true == true)
{
yield return new WaitForSeconds(1);
Debug.Log ("while test working");
}
}
What could possibly be the reason why that Debug.log would not show up every second? I checked the time.time by debug.logging it into the update and it works fine. And if I place the Debug.log infront of the yield, it prints. But it never gets past the Waitforseconds.
Thanks all.
Not that I am using the method in the following script and it works great.
IEnumerator Flip() { while(true) { ranFloat = Random.Range (0.0f, 50.0f); randomRot.x = Random.Range (1.0f,2.0f) - 1.0f; randomRot.y = Random.Range (1.0f,2.0f) - 1.0f; randomRot.z = 0.0f; // Debug.Log (randomRot); flipSwitch = true; yield return new WaitForSeconds(2.0f); randomRot = Vector3.zero; flipSwitch = false; yield return new WaitForSeconds(2.0f); }
}
This is not an answer! I am posting this into answers because commenting does not have formatting for code. This is what I found.
if I run...
void Start(){
StartCoroutine(Generate());
}
it runs through the loop fine and I see all the debugs.log in the loop multiple continuously.
IEnumerator Generate(){
Debug.Log ("Generator is running for "+gameObject.name);
while(true)
{
Debug.Log ("new loop in generator");
yield return new WaitForSeconds(0.5f);
//myParent.Broadcast$$anonymous$$essage("ChangePower",5.0f);
Debug.Log("we got past the 5 second wait");
}
}
does what one would expect. But then if I call that same function out of here:
void SetGenerator (bool incBool)
{
Debug.Log ("set generator has been set to "+incBool+ " for " +gameObject.name );
if(incBool == true)
StartCoroutine(Generate());
}
It does get into the function, it prints out the "Generator is running" log and the "new loop in generator" log so it gets inside the "while" loop but it never prints the "we got past the 5 second wait"
What em I missing here.
No Errors, it gets worse. Once I tried to run the Coroutine from the SetGenerator function it breaks the script permanently. Now even if I comment everything out of the script and only have the start function saying StartCoroutine(whiletest());
And the whiletest has nothing but a loop like while(true) { Debug.log(time.time); yield return new WaitForSeconds(1); } (caps and and misspelling are not in the script just in the comment)
It no longer works. I have to make a new script to get it to work again.
I am sure the incBool is true because the Debug.log before the check tells me so, in addition it prints the first 2 debug.logs before the yield instruction.
And your console say no error or something like that :/ That's weird for me it must work... I'm investigating ^^"
Answer by Demigiant · Nov 02, 2012 at 11:41 PM
Only thing that comes to my mind (apart what Eric5h5 said) is that somehow the MonoBehaviour that started the coroutine was destroyed. Did you check it? Coroutines live and die with the MonoBehaviour that starts them, not with the MonoBehaviour that contains them.
wow, you actually got me there. I did not think that it could possibly be the case because I never delete the game object but on initialize, it turns all children off for a second and then turns on only the object that it needs to. So what happens is that it runs and gets turn off and on again. So to me it does not look like the object is deleted or interrupted but I 4got that I has setup to the setup stage that way. I was so stuck that I thought Unity was going loco so I had reinstalled unity and everything. Thank you for helping me brain storm. Thank you too for your effort Eric. I have not had the need to be back here in a while, I can usually find what I need now but this was driving me loco. I love that Unity has such a strong community.
Ehe, glad I could help with the brainstor$$anonymous$$g. I know how it is when something feels so "sure" that you kick your brains while trying to find the issue elsewhere ;)
I was having a similar problem, and this answer also solved the problem for me! Sorry I can't vote it up (not enough rep)
Just to add a little extra information to this answer, I had a problem with an infinite Coroutine/thread being killed as well. $$anonymous$$y problem was that I placed the call to StartCoroutine in the Awake
method and not the Start
method. As soon as I moved to to the Start
method, it fixed the problem.
Thank you! I spent so long wondering why my coroutine wasn't working, this is the solution.
Answer by Eric5h5 · Nov 02, 2012 at 05:33 AM
Turn off "collapse" in the console. (Number of times I've typed this answer: 500 and counting. ;) ) By the way, you should generally not use quotes in StartCoroutine unless you intend to use StopCoroutine.
Sorry to frustrate you Eric, you have answered many of my questions in the past. But this is not the case. Collapse is not on. There must be something in the rest of the script that is stopping my Coroutine. I have checked for return;s but do not see one that could make the difference. I am going to comment out one line at a time to see where the problem is.
Ok I have an interesting development with this problem. I started a new scrip with just the while loop test which works fine. I set one up that would just print the time every second. Then I started copy and pasting in one element at a time into the test script to see where to problem lies. What that helped me to find was that now I can run the none working loop from the "Start" function but not from the function that it needs to be called from. I am going to post what I mean in an answer because comments do not have formatting for code.
Well, the code you posted works fine and the only possible reason for the Debug.Log not showing up every second in this case is if collapse is turned on.