- Home /
Best way to instantiate lots of objects
Hi, I have a script which at the start of the level instantiates lots of cubes... I'm talking of about 15k cubes at once. Obviously the loading is pretty slow, it takes about 1 or 2 minutes. I would like to know if someone can recommend some better way to instantiate all these objects. Actually, I don't think there are any other methods, since the objects should be instantiated anyway. However, asking shouldn't be a problem , and I could learn something new... So, this is how my script proceeds:
The script checks if player added a seed composed of six alphanumeric characters. If not, it's generated randomly.
Every character of the seed is turned into an integer relying on a sort of list (for example, a = 0; b = 1; c = 3; 1 = 0; and so on...). At the end every integer is stored in a variable.
The script instantiates the first cube at position (0, 0, 0), which is the starting position of the player. After that, is starts generating cubes with the aid of some calculations which calculate x, y, and z of every cube. I will put at the end of this topic a piece of script to make this point more understandable.
After that, all the cubes are made children of an empty object called "Galaxy1".
Points 3 and 4 are repeated twice again, interchanging seeds, naming the groups "Galaxy2" and "Galaxy3", rotating and moving the whole groups so that you get a tangle.
Finally, the script checks if there are cubes with same position, and destroys all cubes at same positions except one, so that there are no straddling cubes. In this way I delete useless cubes and avoid graphical glitches.
There's the piece of script which I talked about in point 3.
nmcube = nmcube + 1;
if (s6 == 1 || s6 == 4){
x = x + 1;
}
else if (s6 == 2 || s6 == 5){
x = x + 2;
}
else if (s6 == 3 || s6 == 6){
x = x - 1;
}
if (s2 + s4 > 10)
{
z = z + 1;
}
else if (s2 + s4 < 5)
{
z = z - 1;
}
if (s6 - s2 < 4)
{
fattore = fattore + (s3 / s5) + 1;
if (fattore == 1 || fattore == 3 || fattore == 4 || fattore == 10 || fattore == 12 || fattore == 13 || fattore == 14 || fattore == 20 || fattore == 22 || fattore == 25 || fattore == 26 || fattore == 30 || fattore == 32 || fattore == 35 || fattore == 37 || fattore == 40 || fattore == 43 || fattore == 45 || fattore == 46 || fattore == 50 || fattore == 58 || fattore == 59 || fattore == 60 || fattore == 70 || fattore == 71 || fattore == 72 || fattore == 73 || fattore == 74 || fattore == 75 || fattore == 76 || fattore == 77 || fattore == 78 || fattore == 79 || fattore == 83 || fattore == 87 || fattore == 88 || fattore == 89 || fattore == 90 || fattore == 100 || fattore == 101 || fattore == 102 || fattore == 104 || fattore == 131 || fattore == 134 || fattore == 138 || fattore == 145 || fattore > 150)
{
y = 1;
}
else
{
y = 0;
}
}
else
{
fattore = (s3 / s5) + 1;
if (fattore == 1 || fattore == 3 || fattore == 4 || fattore == 10 || fattore == 12 || fattore == 13 || fattore == 14 || fattore == 20 || fattore == 22 || fattore == 25 || fattore == 26 || fattore == 30 || fattore == 32 || fattore == 35 || fattore == 37 || fattore == 40 || fattore == 43 || fattore == 45 || fattore == 46 || fattore == 50 || fattore == 58 || fattore == 59 || fattore == 60 || fattore == 70 || fattore == 71 || fattore == 72 || fattore == 73 || fattore == 74 || fattore == 75 || fattore == 76 || fattore == 77 || fattore == 78 || fattore == 79 || fattore == 83 || fattore == 87 || fattore == 88 || fattore == 89 || fattore == 90 || fattore == 100 || fattore == 101 || fattore == 102 || fattore == 104 || fattore == 131 || fattore == 134 || fattore == 138 || fattore == 145 || fattore > 150)
{
y = 0;
}
else
{
y = 1;
}
}
tempo = GameObject.Instantiate (cube[Random.Range (0, 5)], new Vector3(x, y, z), Quaternion.identity) as GameObject;
tempo.transform.parent = groupa.transform;
tempo.name = "Cube" + nmcube.ToString();
if (s6 == 3 || s6 == 6)
{
if (x > -31) {goto Labelone;}
else {goto Labeltwo;}
}
else
{
if (x < 31) {goto Labelone;}
else
{
x = 0 - x;
z = 0;
y = 0;
fattore = 0;
goto Labeltwo;
}
}
Labeltwo:
The variables "s1", "s2", "s3", "s4", "s5" and "s6" are the integers which I talked about above, at point 2.
I recommend you read up on the concept of Voxels, and search around for Unity $$anonymous$$inecraft, as there are many people that have attempted to create something similar in Unity.
This was the first thing that popped up: http://in2gpu.com/2014/07/27/build-$$anonymous$$ecraft-unity-part-1/
Your answer
Follow this Question
Related Questions
Anyone know why this isn't working? 1 Answer
C# GameObjects Instantiate Into Each Other Issue 1 Answer
Three Spots For Three Random Objects 1 Answer
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
Instantiate a GameObject at the position of one of its child objects 1 Answer