# Unity3d: Box collider attached to animated FBX models through scripts at run-time have wrong dimension

I have several scripts attached to static and non static models of my scene. All models are instantiated at run-time (and must be instantiated at run-time because I'm procedural building the scene).

I'd like to add a BoxCollider or SphereCollider to my FBX models at runtime.

With non animated models it works simply requiring BoxCollider component from the script attached to my GameObject. BoxCollider is created of the right dimension. Something like:

[RequireComponent(typeof(BoxCollider))]
public class AScript: MonoBehavior
{

}


If I do the same thing with animated models, BoxCollider are created of the wrong dimension. For example if attach the script above to penelopeFBX model of the standard asset, BoxCollider is created smaller than the mesh itself.

How can I solve this?

Have you tried using Bounds to generate the box collider? You use something like this in runtime:

   Bounds mapBounds = new Bounds (transform.position, Vector3.one);
foreach(Transform child in this.transform) {
mapBounds.Encapsulate(child.gameObject.renderer.bounds);
}


• thanks for the suggestion. I didn't know about that. I'll try your solution as soon as possible. – Heisenbug Oct 22 '12 at 7:47
• sorry..It doesn't work. with rig datas inside the animated models, even with your method the size of mapBounds is wrong. It seems that rig controls point are included while calculating bounds. – Heisenbug Oct 22 '12 at 9:02
• I'm sorry to hear that. Have your tried asking this in unity answers? You might have better luck asking such a specific question there. – Pedro Caetano Oct 22 '12 at 20:10