- Home /
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.
Answer by Bunny83 · Oct 16, 2014 at 09:36 PM
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.
I have now attached a kinematic rigidbody to the main gameObject, and now the trigger function works. But how would I detect which of the triggers was triggered? What people above have suggested only refers to the other gameObject(Player), but I need the name of the trigger itself. Can you suggest anything that works straight out of the main script, so I don't have to have individual script for the 10 triggers? EDIT: Thank you, I actually went with the first approach you suggested. I realised the answer to my question is no(fix if I'm wrong though).
Hi, I have the same problem, after all this time, do you know if the idea you suggested is possible to now?
Yes you're right ;) Again, the trigger messages are generated by the rigidbody and not by a collider. Unfortunately Unity doesn't give you the information which collider of the rigidbodies compound colliders was responsible for the trigger-enter event. The rigidbody sees itself as one thing and just tells you what other thing you've hit.
So if you want to know which of your colliders has hit something you need a rigidbody on each of them.
$$anonymous$$eep in $$anonymous$$d that you can simply use a small "proxy script" on each child, that is just forwarding the event to your main script. Something like this:
private ScriptOnParent parent;
void Start()
{
parent = transform.parent.GetComponent<ScriptOnParent>();
}
void OnTriggerEnter(Collider aCol)
{
parent.OnChildTriggerEnter(collider, aCol); // pass the own collider and the one we've hit
}
Oh yes, thank you so much! Simply adding a $$anonymous$$inematic Rigidbody to the top parent was exactly the solution I needed for my case!
Answer by palash_bhowmick1 · Oct 16, 2014 at 08:25 AM
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); }
Thanks, but I think I confused my question. I edited it once more.
Is there a way to know which child game object was triggered? like getting its name or tag?
Answer by rahme · Oct 16, 2014 at 07:27 AM
if(other.gameObject.tag == "put the name of the tag") Debug.Log("first got triggered");
Thanks, but I think I confused my question. I edited it once more.
Answer by sudoku1v2 · Sep 26, 2020 at 08:58 AM
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.
Is there a way to know which child game object was triggered? like getting its name or tag?