- Home /
How to create and move primitives without the static collider penalty?
Hi,
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.Translate(location);
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?
Answer by whydoidoit · Jun 18, 2013 at 07:26 PM
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);
Thanks whydoidoit.
I tried option c), but I'm a little confused by the results. If I do Debug.Log(cube.collider.ToString()), I get "null", as expected, however the profiler still shows this:
If the colliders no longer exist, why are they still being moved? Is the function automatically called whether a profiler exists or not? Or am I completely misinterpreting what the profiler is showing, and it's actually set_parent() itself that is expensive?
It really looks like it thinks it still has a collider :S Are you sure it's gone in the scene during run?
Set parent is a pretty expensive operation actually - do you have to do that often?
Well, I did a Debug.Log(cube.collider.ToString()) both before and after set_parent(), and got "null" each time, as expected. So the collider really does appear to be gone.
I do set_parent for each primitive I create, but they all have the same parent so if I understand correctly, I can get around that problem by creating the first one, setting its parent, and then instantiating all the others from the first one. From what I gather, if I do that, they will all share the same parent with a single call to set_parent().
I intend to try that, but I don't think it will help with the collider issue, since I still have to translate my cubes anyway.
Answer by brikim13 · Feb 21 at 02:03 AM
I created a simple prefab (without the collider) in my asset folder.
Then I instantiated that object instead.
GameObject cube = Instantiate(MySimpleObject);