- Home /
wierd infinite while loop
I'm trying to find a dynamic amount of objects with the renderer mesh disabled, but the while loop I made make unity crashes.
Can someone explain me why?
int AnotherNumber() { int i;
while(true)
{
i = Random.Range(0, iCounter);
if(Lasers[i].GetComponent<MeshRenderer>().enabled == false)
{
Debug.Log("is disable");
break;
}
else
{
Debug.Log("is enabled");
}
}
Debug.Log(i);
return i;
}
Answer by Eric5h5 · Apr 12, 2014 at 12:32 AM
You wrote an infinite loop, where the only break condition is if at least one of the mesh renderer components in an array is disabled. If none of them are, then the loop will never end, which is definitely the kind of logic you never want to use. Instead of "while(true)", use some kind of condition that will always cause the loop to end.
Your answer
Follow this Question
Related Questions
Quaternion.Slerp issue 1 Answer
Iterator variable not increasing. 1 Answer
How to get something to be inactive while there is a certain value 2 Answers
Display a loading while processing a while loop 1 Answer
While loop freezes unity 3 Answers