Hello,
I have created a loop to spawn in cubes to a certain depth below some Perlin-Noise terrain. The loops will freeze though when i don’t have any of the below in it:
yield WaitForSeconds(0.1);
But makes the blocks spawn in too slowly. Is their anyway to make all the blocks spawn in without crashing Unity?
Code:
for(var child : Transform in transform){ //Creating layers of blocks below the initial layer
var level = child.transform.position.y - 1;
while(level > -6){
var cb = Instantiate(cubeTwo, Vector3(child.transform.position.x, level, child.transform.position.z), Quaternion.identity);
cb.transform.parent = transform;
level -= 1;
Debug.Log(level);
}
yield WaitForSeconds(0.001); //Wait 1 Frame
}
Any advice is welcomed 
Your problem is that what you do actually shouldn’t work and should throw an exception, however the enumerator which Unity implements for the transform class doesn’t do an internal version check which it should!
What happens is that you iterate over the child objects of the transform, but inside the loop you add more childs. That results in an infinite loop. You should save the transform list before you iterate over it.
In C# i might use Linq to pull a copy of the child array, however i’m not sure if and how it works in UnityScript.
That however should work:
var childs = new Transform[transform.childCount];
var index = 0;
for(var tmp : Transform in transform){
childs[index++] = tmp;
}
for(var child : Transform in childs){
var level = child.transform.position.y - 1;
while(level > -6){
var cb = Instantiate(cubeTwo, Vector3(child.transform.position.x, level, child.transform.position.z), Quaternion.identity);
cb.transform.parent = transform;
level -= 1;
Debug.Log(level);
}
}
If you don’t want a delay you don’t need a yield (as long as the loop has an end
)