# Getting child collider when collision occurs in compound object in unity3d?

I have a compound object. The parent of the compound object hierarchy has a script that is listening to the OnTrigerXXX callbacks of the children. When this compound object collides with other objects is there any way to get both colliders? I mean the "other" collider and the collider that belongs to the compound object itself that collided?

P.D: All rigid bodies must be kinematic, so, I can't use OnCollisionXXXX with the collision info param that has all the information I need in the contacts array.

When we are not using compound objects and we want to have a centralized place to handle children collision events, we could have something like this:

//In Children
void OnTriggerEnter(Collider c)
{
(parent's script).TriggerHandler(this, c);
}

//In Parent
public void TriggerHandler(GameObject theChild, Collider c)
{
// do stuff
}


This is ok and will work. The "problems" with this way are:

1. I could have a lot of children, so, using compound objects I would need to add just a rigid body component to the parent. This could be a pretty good memory reduction usage. However if I don't use compound objects I will have to add a rigid body to every child like in the previous code.
2. In my case children under a parent are always touching. Not intersecing but touching. Think about a set of blocks one beside the other. They do not intersect but there are no gaps between them. With compound objects I don't have to mess with it. It authomatically treats all the children as a whole and no collision callbacks will be fired among them.

On the other hand when using compund objects, you get all the advantages listed before but if you need information about what children collided with another collider you won't be able to get it because your script listening to callbacks is in the parent and you will only receive as parameter the "other" collider when unity calls you back through OnTriggerXXXX callback.

I was asking if there is any way to get both colliders using a compound object.

Hope everything is clearer now.

Cheers

• Can you try to explain your concern a bit more because it seems a little unclear to me – Savlon Nov 29 '14 at 11:03
• I have added a bit more of information to the question. – Notbad Nov 29 '14 at 12:26

You could, at runtime, assemble your children's colliders into one big collider that you add to the parent. In order for this to work the way I've done it below, the children would have to have Mesh Colliders, even if they were box-shaped:

// Use this for initialization
void Start () {
MeshCollider[] childMeshes = GetComponentsInChildren<MeshCollider>();

CombineInstance[] combine = new CombineInstance[childMeshes.Length];

for (int i = 0; i < childMeshes.Length; i++)
{
combine[i].mesh = childMeshes[i].sharedMesh;
combine[i].transform = childMeshes[i].transform.localToWorldMatrix;
}

Mesh mesh = new Mesh();
mesh.CombineMeshes(combine);