Get OnTriggerEnter of a child.

Hello. I have a gameObject with a script, and 10 child gameObjects, which all have triggers on them. I really need help in acquiring OnTriggerEnter information from each trigger individually, from the main gameObjects script. Now this is not as Compound Collider, as I need to see which trigger has been triggered.

I really don’t wish to make a script for all 10 of the children, as I feel that is the wrong way of doing it.

EDIT: I didn’t explain it clear enough in the first time. What I need is a function on the main gameObject(which doesn’t have any colliders) which can show if a child trigger has been triggered. Because the current OnTriggerEnter() function in the main gameObject doesn’t work, because it lacks a collider/trigger on the gameObject, but it’s children don’t. bump.

Big thanks in advance.

The of the main gameObject script:

var firstTrigger : Transform;
var secondTrigger : Transform;
...
var tenthTrigger : Transform;

//this doesn't work, but is example of how I need it to be
function OnTriggerEnter(other : Collider) {
	if(firstTrigger)
        Debug.Log("first got triggered");
	if(secondTrigger)
        Debug.Log("second got triggered");
	...
	if(tenthTrigger)
        Debug.Log("tenth got triggered");
}

Or something in that direction. I’ll appreciate any kind of help. I have been searching thoroughly and really couldn’t find nothing to help me.

You have to understand one fundamental thing about triggers. Triggers don’t create OnTriggerXXX messages, only rigidbodies do. That’s why you need at least one rigidbody to even have such messages firing. You said the child colliders are not compound colliders, but they are, you can’t prevent that. The only way to know which of the child triggers got “triggered” is:

  • put a kinematic rigidbody on each of your child objects and a script that receives the corresponding OnTrigger messages.
  • put the script on the other object which you want to detect. That way you get the trigger message on that object with a reference to the child collider.

Personally i would go with the first approach but it depends on your actual needs / setup.

if(other.gameObject.tag == “put the name of the tag”)
Debug.Log(“first got triggered”);

It is better to use OnTriggerEnter function in the Object which will be triggered. By that way, you can get the info of which child it is triggering…

function OnTriggerEnter(other : Collider)
{
Debug.Log("Triggered on: "+other.gameObject.name);
}

And after some test on Unity 2019.4.10f1:

To trigger the Ontrigger function in the object script :

  • at least, the object or the collided
    object must get a Rigidbody,

  • at least, the object or the collided
    object must get their Box Collider
    with “IsTrigger” set,

  • a child object inherits the Rigidbody parent

  • if trigger is passing through the parent (Rigidbody in parent and not in child), then the parent is also trigged.

  • if child has a rigidbody, collision between child and parent will generate a trigger.

Well, quite complicated if you use Box colliders in both Parent and children…

I have to think about for my game to avoid unexpected behaviour.

Cheers.