- Home /
OnTriggerEnter not firing if I instantiate a GameObject completely inside another's trigger
I have the follow setup:
GameObject A, with a CharacterController, and a child GameObject Ac, containing a sphere collider (radius 10, for example purposes) with Is Trigger set to true.
GameObject B, with a CharacterController, a a child GameObject Bc, containing a sphere collider (radius 2) with Is Trigger set to true.
Ac and Bc both contain the same script that, for now, simply has a method for capturing OnTriggerEnter.
If A and B move towards each other OnTriggerEnter works fine. If I instantiate a copy of B such that the character controller is intersecting with Ac, OnTriggerEnter fires for both.
However, if I instantiate a copy of B such that the character controller of B is entirely contained within Ac's sphere collider, things don't work as expected. Sometimes Ac's OnTriggerEnter fires, and sometime's Bc's OnTriggerEnter fires, but not both.
I've been trying to figure out why this is the case, or if there's any workarounds. Any help would be greatly appreciated :)
Answer by Aram-Azhari · Feb 04, 2012 at 09:03 AM
If a collider is already contained inside another collider, the OnTriggerEnter won't fire. The only trigger that is operating at this situation is OnTriggerStay.
Let me know if this answers your confusion.
I tried that as well and the same thing seems to happen. If I Instantiate B such that the CharacterController is entirely contained with Ac's sphere collider (trigger) then OnTriggerStay fires for:
A's CharacterController triggering Ac's collider
B's CharacterController triggering Bc's collider
B's CharacterController triggering Ac's collider.
But A's CharacterController never seems to trigger Bc's collider.
Can you provide a package that produces this problem? That way we may have a better chance of finding the problem.
I think I managed to solve it. Adding a kinematic rigid body to Ac and Bc resulted in the correct behaviour, and the triggers fire for all cases, not just 3 out of 4. Thank you very much for the help and attention though!
Answer by UnPluks · Mar 09, 2016 at 02:33 PM
I know this is old, but in case some one is still looking for the answer. One way I do it is to Disable and enable the collider right after instantiating it.
GameObject obj = Instantiate(prefab) as GameObject;
obj.GetComponent<Collider>().enabled = false;
obj.GetComponent<Collider>().enabled = true;
Hope this helps someone :D
Version with variable cache to avoid one extra GetComponent call:
GameObject obj = Instantiate(prefab) as GameObject;
Collider collider = obj.GetComponent<Collider>();
collider.enabled = false;
collider.enabled = true;
Answer by PropanBen · Sep 09, 2018 at 10:28 AM
I had the issue when i lay an item to the ground i instantinate it from prefab on the ground and then i wanted the possibility to pick it up right again. The collider OnTriggerStay does not recognize it. So i have a working workaround for this where i move the object with the collider a little bit after instantinating, so the collider will recognize it again.
Move the Object with the Collider like this.
transform.position = new Vector2 (transform.position.x+0.000001f, transform.position.y);
Thanks this worked for me, both with OnTriggerEnter and OnTriggerStay
Your answer
