- Home /
Had difficulties implementing intro to Coroutines from unitypatterns.com. Help?
I can't figure out the cause of the problem. I've tried restarting Unity and rebooting my PC. I've tried passing string and method names. Switching 0 to null made no difference. Nothing ever changes. I'm always consistently getting either 1 Hello when yield comes before Debug.Log, and 2 Hellos when yield comes after. Any ideas??? I literally only put this online to ask this question here. I really think I need to have Coroutines work properly, I don't know what the source of the problem is.
Gist with syntax highlighting: https://gist.github.com/nastajus/e420713c2aa3be04b8cb
Github entire but simple project: https://github.com/nastajus/CoroutinesLearning
Unity Patterns tutorial: http://unitypatterns.com/introduction-to-coroutines/
The most interesting result was when I append i to the output it prints 5 times instead. Very weird and confusing based off the tutorial.
void Start(){
StartCoroutine("SayHello5Times"); //doesn't work right
}
IEnumerator SayHello5Times(){
for (int i = 0; i < 5; i++){
//instructions: comment out other block
yield return 0;
Debug.Log("Hello"); //prints once
//Debug.Log("Hello " + i); //prints 5 times
}
}
Answer by SirCrazyNugget · Jul 04, 2014 at 04:39 AM
The good news is your code works perfectly, the bad news is you're about to kick yourself. (I think anyway)
In the console you have an option to "Collapse" repeated items. I'm guessing you've got that on so only seeing repeated "Hello"'s only once though they have a counter on the right as to the number of times they've appeared. With the other lines with the appended i's as the comments are unique they're individually listed.
PEB$$anonymous$$AC. sigh. I didn't see the little counter that accumulates all the identical messages. There's 92 hello's compounded in the image I posted on my github. I finally just noticed the little compound counter on the right side of the hello line. Thank you!