- Home /
Infinite Looping Crash
I'm having a strange problem in my scripts... I have one function that is:
function Fire()
{
while(true)
{
yield WaitForSeconds(delay);
Instantiate (bullet, transform.position, transform.rotation);
}
}
This one works fine (BTW I call it in start, not update). Now this one crashes my game every time I load it:
function SpawnEnemy() { while(true) { if(enemyCount < limit) { yield WaitForSeconds(delay);
random = Random.Range(lowerBound,upperBound);
switch(random)
{
case 1:
if(firstPoint != null)
{
x = firstPoint.transform.position.x;
y = firstPoint.transform.position.y;
rotate = firstPoint.transform.rotation;
time = 0;
}
break;
case 2:
if(secondPoint != null)
{
x = secondPoint.transform.position.x;
y = secondPoint.transform.position.y;
rotate = secondPoint.transform.rotation;
time = 0;
}
break;
case 3:
if(thirdPoint != null)
{
x = thirdPoint.transform.position.x;
y = thirdPoint.transform.position.y;
rotate = thirdPoint.transform.rotation;
time = 0;
}
break;
case 4:
if(fourthPoint != null)
{
x = fourthPoint.transform.position.x;
y = fourthPoint.transform.position.y;
rotate = fourthPoint.transform.rotation;
time = 0;
}
break;
}
Instantiate(enemy,Vector3(x,y,0),rotate);
enemyCount ++;
}
}
}
I have no idea what the problem is...
Answer by duck · Apr 19, 2010 at 10:14 PM
It's because you're not yielding at all if your enemyCount isn't less than limit, which results in a tight 'while true' loop that does nothing at all, but never releases execution back to the rest of your game.
Move your yield up a little, so it comes before that 'if' statement, just after the opening bracket of the while loop.
Don't worry, I checked you both so you had credit in your accounts.
Answer by Michael La Voie · Apr 19, 2010 at 10:14 PM
The second script is crashing because as soon as the enemy count is not less than the limit, it goes into an infinite while loop.
The code inside the while checks that condition, then does it again. It never releases control back to the game engine to perform all the other functions necessary to make a game.