• 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
0
Question by solocs · Jul 21, 2019 at 04:45 AM · ontriggerexit

How to not trigger OnTriggerExit2D when an Object is destroyed?

I have a Player object but the items I'm picking up are triggering the OnTriggerExit2D of my player script whether it gets destroyed or disabled.

This characteristic is very annoying and I believe that if an object is destroyed, it shouldn't trigger OnTriggerExit. I've tested this on 3D OnTriggers and it doesn't do this. How do I disable this?

Comment
Add comment · Show 1
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 rahulpatil6220375 · Jul 21, 2019 at 04:54 AM 0
Share

not sure but you can call after

void OnTrigerEnter2D() {

}

void OnTriggerExit2D() { //trigger exit
}

void OnDestroy() { //all action performed and last this one called onDestroy you can use courutione }

sure it is work use courutine

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_21erushbrook · Jul 21, 2019 at 04:52 AM

Just check if isActiveAndEnabled is true on the object that's exiting. If it is, just return out of the function.

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 solocs · Jul 21, 2019 at 05:14 AM 0
Share

it does not work at all

avatar image
0

Answer by MANA3DGames · Jul 21, 2019 at 09:01 AM

Neither OnTriggerExit nor OnTriggerExit2D should be triggered by a destroyed object.

Are you sure that the picked item is the one who triggers the OnTriggerExit2D of the player? Are you destroying the picked item immediately? or after few seconds? Destroy( itemGameObject, DestroyAfterSomeSeconds );

if you are destroying the item immediately before it leaves the trigger area of the player then this should not happen.

Here is a few steps could help you debugging this issue:

In your Player script add the following

 private void OnTriggerEnter2D(Collider2D collision)
     {
         if ( collision.tag == "Item" ) // replace it with your item tag
         {
             Debug.Log( "I'll pick up this item so you can destroy it NOW!" );
             Destroy( collision.gameObject );
         }
     }

 private void OnTriggerExit2D( Collider2D collision )
             {
                 if ( collision.tag == "Item" )
                 {
                     // If everything is working right then you should not see this line.
                     Debug.Log( "Yes this is the picked item: " + collision.gameObject.name );
                 }
             }

In your Item script add the following

     private void OnDestroy()
     {
         Debug.Log( "I was destroyed by the player!" );
     }

Ideally, you should see the following lines in your console (in this exact order):

"I'll pick up this item so you can destroy it NOW!"

"I was destroyed by the player!"

Otherwise something else is calling the Player OnTriggerEnter2D

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

111 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Varaible not decementing OnTriggerExit 1 Answer

OnTriggerEnter, OnTriggerExit Work Only Once 2 Answers

Character Controller not detected OnTriggerExit 0 Answers

Trigger on MeshCollider Only Seems To Work at Edges of Mesh 1 Answer

OntriggerEnter / Stay with the same Gameobject tags 0 Answers


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