- Home /
Is this loop infinite?
I'm trying to write a loop to build a grid of tiles. Unity crashes when I start the game. No log errors, just breaks. Have to kill it. So I'm assuming this loop is infinite. I can't see why, but I think I could be missing something simple, but fundamental.
Any explanations of what I am doing wrong would be great. :) Thanks
var wi : int = 2;
var ex : int = 2;
var cell : GameObject[4];
var CellPrefab : GameObject;
function Start () {
StartCoroutine(BuildBoard());
}
function BuildBoard () {
for(var i=0; i<ex; i++){
for(var n=0; n<wi; n++){
var y = 0;
y = y++;
cell[y] = GameObject.Instantiate(CellPrefab, Vector3(i, n, 0), Quaternion.identity);
}
}
yield WaitForSeconds (200);
}
Answer by gregzo · Apr 20, 2012 at 10:30 AM
1) var y = 0; y++; will always give you y=1! 2) are you sure you want to wait for 200 seconds?
Good point. Fixed the y counter. That doesn't seem to be the crash problem though.
I don't really want to wait 200 seconds, that was just in case it was continuously calling the function. Which it wasn't, of course.
@gregzo: As i thought it will be 0 all the time and not 1 ;)
The problem is the order in which the operations are executed:
y = y++;
- assign a value to y
- evaluate this value (y++)
- remember old value
- increase by one
- return old value
- assign the returned value to y
I would be "1" with a pre-increment:
y = ++y;
But it would still make no sense to write it this way :D
Those pre and post increment / decrement operators can give you headache. Think of this:
y = 1;
x = y++ + y++;
x will be "3". First y++ returns 1 the second returns 2 because of the first increment but y itself is also 3 after this.
Answer by Bunny83 · Apr 20, 2012 at 10:33 AM
I don't see any reason for this code to break. The only obvious thing is that you declared the "y" var inside the inner for-loop so for each iteration it's reset to 0.
Also y = y++;
is a really strange expression and dependend on the implementation can give you undesired results. "y++" is already y = y + 1
. It's the post increment operator which means the current value is returned and then it's incremented. Assigning the old value back to y it very strange.
Anyway, I'm pretty sure that doesn't crash your application. Maybe you have a faulty script on the CellPrefab?
Just to fic the loop:
function BuildBoard ()
{
var y = 0;
for(var i=0; i<ex; i++)
{
for(var n=0; n<wi; n++){
cell[y++] = GameObject.Instantiate(CellPrefab, Vector3(i, n, 0), Quaternion.identity);
}
}
yield WaitForSeconds (200);
}
edit
:D Just realised the 200 seconds ;) This won't crash the application but it might take a while.
What can i say, I'm strange. Also, fairly inexperienced at program$$anonymous$$g. The 200 seconds was just to see if it helped, which it didn't. Unity crashes regardless of timer. Crashes instantly. I've created a scene with just a blank mesh of a prefab, this script(with your fix), and nothing else. Still crashes.
I take it I must assume the problem lies in Unity?
Ok, just found two more things, but they actually would prevent the script from compiling:
var cell : GameObject[4];
An array-type can't contain a size. The size have to be specified when the array is created
You didn't create the array which would give you a "index out of bounds" exception at runtime.
Either create the array right here: var cell : GameObject[] = new GameObject[4]; // or var cell = new GameObject[4];
or initialize the array in your build function which is the better way since it can be calculate the size :
var cell : GameObject[];
// [...]
function BuildBoard ()
{
cell = new GameObject[ex * wi];
Your answer
Follow this Question
Related Questions
Unity crashes because of script 2 Answers
Infinite Looping Crash 2 Answers
A node in a childnode? 1 Answer
Unity crashes when using while 2 Answers