I try to instanciate 200 cubes at start up. They are all simple primitives with a material. But my problem is that it takes like 10s to start! And my computer is not slow! :0
var cube : GameObject;
function Start () {
for(var z = 0;z< 2;z++)
{
for(var x = 0;x < 10;x++)
{
for(var y = 0;y < 10;y++)
{
cube = Instantiate(cube,Vector3(x,-z,y),Quaternion.identity);
}
}
}
}
Thx
Format code properly using the code button. Also post more details, because that code on my computer takes .007 seconds. (Also, assigning Instantiate to a variable is pointless if you're not using that variable for anything, so you can leave it out.)
Instead of instantiating the same cube prefab 200 times, you’re copying the previously instantiated cube each time (because you assign it back to the cube variable). It’s possible that that’s creating the slowdown, and it’s certainly not a useful thing to do.
Yep, alot people doesn't realise that Instantiate does something like CTRL+D in the editor. It clones the given object. The "source" object could be a prefab (off scene object) or an actual object in the scene.
I’m using something to do the same thing but i use Update function. If you can use it in your game,forget about Start
var CubeNumber = 200;
var cube = GameObject; // cube prefab
function Update(){
if(CubeNumber>0){
Instantiate(cube,Vector3(x,-z,y),Quaternion.identity); // Instantiate cube all the time until CubeNumber is 0.
CubeNumber-=1;// when 1 cube is created, substract 1 from CubeNumber
}
}
That would take 200 frames to instantiate all the cubes, which probably isn't acceptable in most cases. There's no reason anyway, since it takes a tiny fraction of a second to instantiate 200 cubes.
Format code properly using the code button. Also post more details, because that code on my computer takes .007 seconds. (Also, assigning Instantiate to a variable is pointless if you're not using that variable for anything, so you can leave it out.)
– Eric5h5