Hi all , I find myself using the following trick to stop unity from crashing when a while loop goes infinite .
void WhileTest() {
int limit = 0 ;
a = Random.Range(0,6);
GameObject ActiveSide = Sides[a] ;
// this is to find an inactive object , its for a custom , yet crude pooling system
while(ActiveSide.active == true limit < 20 )
// the limit is the tick, without it if we never find an inactive object then Unity crashes
{
// goto Restart ;
if(ActiveSide.renderer.IsVisibleFrom(Camera.main)){
a = Random.Range(0,6);
ActiveSide = Sides[a] ;
limit++;
}
limit++;
//yield return new WaitForSeconds (1);
}
}
Of course any while loop crashes could be avoided if I could write perfect code that always finds what its looking for .
But what I do is here is create a int , limit , thats increased each time the while loop is ran , this way if for some bizarre reason we cant find an inactive object we don’t crash .
If you know the amount of time you want the loop to run, use a for loop:
void WhileTest()
{
a = Random.Range(0,6);
GameObject ActiveSide = Sides[a] ;
// this is to find an inactive object , its for a custom , yet crude pooling system
for( int i = 0; i < 20 ActiveSide.active; ++i )
{
if ( ActiveSide.renderer.IsVisibleFrom(Camera.main))
{
a = Random.Range(0, 6);
ActiveSide = Sides[a];
}
}
}
And the syntax is:
for( int i = 0; i < 20; ++i )
You have for( variable initialization (these are only available in the for loop); terminating conditions; what to perform after each loop (used usually to increment the variables you made in the initialization)).
You can always take some cracks at using them and if you run into issues just post here and I’ll help you out.
What I basically do here is check to see if the object I selected is active or not , if it isn’t active the loop ends and we just select that object .
If it is active , we keep the loop going .
The problem is sometimes all the objects are active, so I need to put a limit to how many times we run the loop .
So i don’t really know how many times we’re going to run the loop until i run it . …
Don’t get me wrong, if all goes well I should never need it , but its a good thing to keep around so instead of the game crashing, the loop terminates .
Why did you have to change my code? You realize that
if ( x ) { }
and
if ( x == true ) { }
are exactly the same, right?
Also, if I understand your code correctly, then you are iterating through to try and find the first object that isn’t visible from the main camera? You can do something like this:
for( int i = 0; i < Sides.Length; ++i )
if ( !Sides[i].renderer.IsVisibleFrom(Camera.main) )
ActiveSide = Sides[i];
Actually I didn’t know that , i’m still new with C#, thanks for the tips .
My game is a little procedural generated avoid objects game , so after we don’t see a gameobject anymore I set it back to inactive , then place in forward in the track .
I already had this working with Intestate and destroy, but since that’s a performance nightmare on android I’m writing this code to reuse the road pieces .
Not exactly, I’m actually moving the camera and the ship up , and when a object is far enough from the ship , I run a script on it to call the PlaceUpSide method . and create a new object with the same position, aside from an extra Y .