Coroutines Madness ();
Hello folks, I've come with coroutines madness:
First Question is Understanding WHY the MainCheckRoutine() is executed twice......
private bool ready;
private float customTime;
void Awake()
{
ready = false;
}
void Start () {
customTime = 0.8f;
StartCoroutine( ELbuscon());
}
void Update()
{
customTime -= Time.deltaTime;
if (customTime < 0)
{
if (ready == false)
{
GameObject[] fs = GameObject.FindGameObjectsWithTag("litrick");
GameObject[] ts = GameObject.FindGameObjectsWithTag("estrello");
GameObject[] rs = GameObject.FindGameObjectsWithTag("match");
if (fs.Length > 0 || ts.Length > 0 || rs.Length >0)
{
//nothing
}
else
{
ready = true;
}
}
}
}
IEnumerator ELbuscon()
{
while(ready == false)
{
yield return new WaitForFixedUpdate();
}
MainCheckRoutine();
}
void MainCheckRoutine(){
Debug.Log("why am i exectude twice?");
SomeCheckingThingFunc();
FinalStep();
}
void FinalStep(){
if(something==else){
StartCoroutine(Elbuscon());}
// THIS Freezes out everything
}
The fact is that if i swtich Start function to IEnumerator Start(){}. and i make some yield inside.... the code just runs once....... makes no sense on me because even Coroutines play independient its supose to yield the instructions after ready = true is met....... Just once becouse Start is fired once... dont understand yah! :(_
Second Question is about looping and freezing. Which would be the best approach to loop the Elbuscon() function after a requirements on the very end of this fucntion; like FinalStep(); this way seems to freeze unityeditor couse a infinite loop even it's not.(my guess...)
Thank You !
Answer by Bunny83 · Jul 28, 2016 at 01:19 PM
Well, you have an infinite chain of method calls. The reason is that your "ready" variable is true when "ELbuscon" runs "MainCheckRoutine". MainCheckRoutine will call FinalStep and final step immediately runs a new "ELbuscon" coroutine. However since ready is still true the coroutine immediately calls "MainCheckRoutine" again, endlessly.
So you're caught in an infinite recursive function call chain. which eventually would cause a stackoverflow. However since each coroutine also allocates memory it might take a while. Maybe you would even run out of memory first ^^.
Yes, Thats for the second question, when FinalStep() runs it goes infinite loop.... i was asking for the approach to get that. And for the First question, Figure it FinalStep() is erased........ it still runs $$anonymous$$ainCheckRoutine() 2 times even after the while statement it should be only fired ONC$$anonymous$$.. Any clues?