- Home /
Instantiate Loop Crashing
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 :)
What is the purpose of waiting for one frame? you may use
yield null;
for the same principal not sure about Js syntax, it is yeild return null; in C#.
But I don't get the point of waiting for so few, you might as well just make it happen at once.
Ins$$anonymous$$d of instantiating here, do it earlier (just after user starts your game) and keep inactive cubes in a generic list. And when needed, just activate them, move to desired position and set parent.
@fafase - I think you're referring to while
loop, but there's also a for
loop. I guess the main transform has many children.
@ArkaneX - This loop happens in the
function Start () {}
@stuart6854 - I thought so :) But I'm writing about creating cubes earlier.
Let's say, an user runs your game. He sees some splash screen, then some start screen, and then after he hits play, your terrain is displayed and cubes are generated. $$anonymous$$y advice is to create cubes earlier, e.g. in start screen, using for example empty object with generation script attached. When user hits Play, you can retrieve cubes and do anything you want with them.
Answer by Bunny83 · Oct 11, 2013 at 04:24 PM
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 ;) )
Is that why it crashes without waitforseconds (because of infinite loop?)?
Wow,
that code worked, except any size bigger than 10x10 freezed the game for about 5 seconds then works, but i guess i could hide that in a loading screen.
Thanks :)