How do you detect which child collider was hit?

Say you have a parent game object with a collider and rigidbody, and two children objects each with their own collider (but no script or rigidbody on each child). If you have a script on the parent with OnCollision/OnTrigger methods, how do you check to see which of the child colliders was hit when there’s a collision?? I’ve seen this question asked about 10 different ways, and none of the questions have an answer.

Just use collision.GetContacts(), each ContactPoint generated has both colliders accessible (thisCollider, otherCollider)

The easiest way in my personal experience in unity is to add vars for the objects

**var child1 : Monobehavior**;//I think GameObject can work as well, but it more complicated

var child2 : MonoBehavior;

now assign them in the inspector.

Then add a public getter for a variable wasHit

in the child

var wasHit : int; //or bollean
function OnCollisionEnter(hitter : Collision)
{
this.wasHit ++;

}
public function wasHit() : int
{
**return this.wasHit;**
}

now from the original script you can do something like

var wasChild1HIt : int = child1.wasHit();

You’re assigning that script as an object, saving a great deal of hassle with the usual marshaling crud.
Other alternatives can include using a public static singleton class to store event states and trigger states(Honestly I do it a lot, since i used a home made toolkit in c++ so very much)This has a solid advantage in that you can always access more or less anythign by just doing this like,

wasChildHIt = Toolkit.wasChildHitGlobal;

but remember, the beauty of unity is that it gives WAY simple methods of doing this sort of thing, without really beating up your performance.

nope it cannot be done i was having the same problem, you can achieve it if you attach script to every game object

void OnCollisionEnter2D(Collision2D coll)
{
// Tag of the object this object collided with
Debug.Log(coll.collider.tag);

     // Tag of the child of the parent object this script is attached to
     Debug.Log("OTHER COLL " + coll.otherCollider.tag);
    }

This seems to be an old topic and I had no idea so many people struggled with it.
Actually the solution is very easy:


Don’t check which child was hit, rather tell the parent which child was hit.

Now this doesn’t answer the question directly - instead its how parent interaction/child should be done.

I know its still confusing but keep reading and this will make sense.


Imagine you are making a shooter where it matters where you get head. Headshots do more damage and legshots do less.
What you want to do is have your main player scripts and functions like Move, Do, Damage etc on the parent.

You then child the different parts to the player object.

– Player [Main Scripts]
---- Head
---- Left Leg
---- Right Leg
---- Torso
---- Arms
---- Scrotum

The main player object will not have a collider, instead we put the colliders on the child objects.

That way when a part gets hit we can simply do a call to the Main Script from the OnCollisionEnter function
in the child scripts. Example:

// Main Player Script
public class Player: MonoBehaviour {

	public float health;
	
	void Start() => health = 100;
	
	public void DoDamage(float damage){
		
		health -= damage;
		
		if (health < 0)
			Die();
		
	}
}

// Child Scripts
public class Limb : MonoBehaviour{
	
	Player player;
	public float damage;
	
	void Start() => player = parent.GetComponent<Player>();
	
	void OnCollisionEnter(Collision col){
		if (col.gameObject.tag == "bullet")
			player.DoDamage(damage);				
	}
}

Obviously quite a late answer, but just in case someone comes across this, in the current version of Unity you can look at Collision.collider.gameObject.name or .tag to see which collider was hit. Got the answer from Weston_2.