I need to fill a box with more sphere . For do this operation i want kno which can be the best solution?
I have used Instantiate() in a for cycle, but when i do this all objects collide and go out of the box. i not need mandatory to do this at runtime but i don’t know otheer way.
Can anyone help me?
To avoid this collision issue, you could instantiate the objects at the top of the box and let them fall. You could use a grid pattern to instantiate a row of spheres, yield, instantiate another row, yield again, and so on. NOTE: this loop should not be in Update, because it doesn’t accept the yield instruction, but could be in Start - something like this pseudo code:
for (var q = 1000; q > 0; ){
for (var i = 0; i < 10; i++){
for (var j = 0; j < 10; j++){
pos = Vector3(i * sphereDiameter, someHeight, j * sphereDiameter);
Instantiate(spherePrefab, pos + box.position, Quaternion.identity);
q -= 1; // decrement counter
}
yield; // let Unity breath until next frame
}
}