Destroying Rigidbody component (Actor::updateMassFromShapes error)

Hi everybody,

in my scene i need to attach to my 3D model a collider component with rigidbody to catch some collisions. I attached box colliders to my model and then a rigidbody component. After the collisions are catched i dont need rigidbody component anymore. And i need to change box colliders to mesh colliders.

if (model.transform.childCount != 0) {
			for (int i=0;i<model.transform.childCount;i++) {
				if (model.transform.GetChild(i).gameObject.GetComponent<MeshRenderer>()!=null) {
					GameObject.Destroy(model.transform.GetChild(i).gameObject.GetComponent<Rigidbody>());
					GameObject.Destroy(model.transform.GetChild(i).gameObject.GetComponent<BoxCollider>());
					model.transform.GetChild(i).gameObject.AddComponent<MeshCollider>();

				}
				
				
			}
		}

but when i run this script it gaves me this error
Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor’s mesh shapes! Please change mesh geometry or supply a tensor manually!
UnityEngine.GameObject:AddComponent()

its caused by this line model.transform.GetChild(i).gameObject.AddComponent();

When i run my script without this line everything works fine … and when i attach a mesh collider in editor during runtime there is no problem.

Any help ?

Thanks

My guess is that creating a MeshCollider from scritp is NOT the same as doing it in the editor. My guess for fixing this would be to reference the mesh that needs the collisions mesh:

 model.transform.GetChild(i).gameObject.AddComponent<MeshCollider>();
 model.transform.GetChild(i).gameObject.GetComponent<MeshCollider>().mesh = model.transform.mesh;

A mesh collider is a very processor heavy thing, I’d ratehr recommend you use a primitive collider fot it.

Rigidbody is a tricky thing. But if you want to use it DON’T not use it at times. That is wrong thinking. It is one of the few things that cannot be disabled, and it’s that way for a reason.

Either just use regular collisions with OnCollision(), where you don’t need rigidbodies or just keep the rigidbody.

Also a tip:

foreach(Transform child in model.transform)

returns model’s transform and all it’s child transforms, that you can reference through child.