Instantiating many objects

Hi,

I am attempting to create a procedural sort of map built out of many cubes. I’ve created a cube prefab and using the following code to instantiate inside a double loop:

UnityEngine.GameObject o = (UnityEngine.GameObject)UnityEngine.GameObject.Instantiate (r, new Vector3 ((float)i, map[i,j] * 1.5F, (float)j), Quaternion.identity);

o.transform.parent = mapRoot.transform;

Creating such a 50X50 map takes a long time. I’ve profiled and it takes 5-6 ms to create the object and 5-6 ms to set the transform. Does that make sense? It makes the map load extremely slow, and this is not even close to the map size I want to have.

50x50 cubes should not take long to instantiate. It should be ~instant.

Btw, when you call o.transform.parent = mapRoot.transform, you are actually calling two GetComponent(Transform)'s and those are somewhat heavy operations.

So what you should do is:

  1. Store all the instantiated objects as Transform instead of GameObject.
  2. Store the mapRoot as a Transform, so you dont have to mapRoot.GetComponent(Transform).