Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Terminas · Feb 04, 2012 at 03:07 AM · instantiateontriggerenter

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Terminas · Feb 04, 2012 at 05:25 PM 0
Share

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.

avatar image Aram-Azhari · Feb 04, 2012 at 05:46 PM 0
Share

Can you provide a package that produces this problem? That way we may have a better chance of finding the problem.

avatar image Terminas · Feb 04, 2012 at 08:59 PM 0
Share

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!

avatar image
7

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fressbrett · Apr 14, 2020 at 03:09 AM 0
Share

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;

avatar image
1

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);

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malkere · Feb 21, 2019 at 01:29 PM 0
Share

This won't however affect OnTriggerEnter in any way

avatar image JoschJava · May 06, 2020 at 12:47 PM 0
Share

Thanks this worked for me, both with OnTriggerEnter and OnTriggerStay

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instatiate inside OnTriggerEnter gives unwanted delay 1 Answer

Make gameobject appear on collision 1 Answer

Instantiate spawns 2 instead of 1, help? 2 Answers

Instantiate prefab, add to list, destroy older prefab 1 Answer

instantiate within OnTriggerEnter, c# question. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges