How to create and move primitives without the static collider penalty?


I need to create a lot of primitives, place each primitive where it needs to go, and make each primitive a child of the same common parent object. Currently, I do just that:

private void CreateMyGameObject(Vector3 location) {
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.parent = commonParent.transform;
    // Some other, irrelevant stuff

The problem is that this takes quite a bit of time (I need anywhere between a few to about 20,000 of those primitives) and according to the profiler, much of the cost comes from moving the static collider associated with each primitive. Apparently, said collider is created along with the primitive by CreatePrimitive().

But I don’t really need this collider, nor do I need it to be static. How do I get around this problem? I figured adding a kinematic rigidbody to the object might do the trick, but it incurs extra work, memory allocation, and doesn’t even appear to help.

If I could disable the creation of the collider, or ideally defer it to the point where the primitive has already been moved, that would be great. Is that possible?

Adding a kinematic rigidbody will remove the physics penalty - but I guess you should either:

a) Create a cube using something apart from primitives (a cube mesh you import perhaps?), create a prefab and make sure it doesn’t have a collider then instantiate that.

b) Create a cube mesh on the fly using new Mesh() and add it to a game object with a Mesh Filter and a Mesh Renderer.

c) Destroy the collider attached to the primitive with Destroy(cube.collider);

I created a simple prefab (without the collider) in my asset folder.

Then I instantiated that object instead.

GameObject cube = Instantiate(MySimpleObject);