• 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 Bluestrike · Sep 24, 2013 at 04:44 PM · ontriggerexit

OnTriggerExit is not called whthen object inside is destroyed

OnTriggerExit is not called when an object that is inside a trigger is destroyed. Is this a bug or intended behavior?

Comment
Add comment · Show 2
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 clunk47 · Sep 24, 2013 at 05:24 PM 1
Share

OnTriggerExit is fired when your object actually leaves, or steps outside the bounds of your trigger collider. Try checking if your object is null ins$$anonymous$$d. You could use a "dummy" variable, or whatever you wish to name it, and set that Collider as what is inside your trigger. Here I use OnTriggerEnter to set the bool and dummy variable. Then I use OnTriggerExit in case your object leaves before it is destroyed, then I use Update to check if it has been destroyed before it leaves.

 using UnityEngine;
 using System.Collections;
 
 public class example : $$anonymous$$onoBehaviour
 {
     Collider dummy;
     bool triggered = false;
     
     void OnTriggerEnter(Collider other)
     {
         dummy = other;
         triggered = true;
     }
     
     void OnTriggerExit(Collider other)
     {
         if(other == dummy)
         {
             print ("Collider left the trigger without being nulled first.");
             dummy = null;
             triggered = false;
         }
     }
     
     void Update()
     {
         if(triggered)
         {
             if(dummy != null)
                 print ("Collider is inside.");
             else
             {
                 print ("Collider has been destroyed, or is inactive while inside trigger.");
                 dummy = null;
                 triggered = false;
             }
         }
     }
 }


avatar image felixmann · May 27, 2017 at 03:09 AM 0
Share

This was a bug that has been fixed in Unity 5.6.

3 Replies

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

Answer by Owen-Reynolds · Sep 24, 2013 at 06:43 PM

It's probably intended. If OTEx was called on destroy, the trigger would get OnTriggerExit(Collider col) with col equal to null (since it was destroyed.) It would be a pain to call OTEx for all triggers, then destroy it. But even then, if col was the "about to be dead" object, OTEx wouldn't have a good way to distinguish a movement exit vs. a destroy exit. And I often want them to do different things.

In practice, you move around a lot and can't hand-check Exit each frame. So having OTEx automatically called is good for movement. But you only get destroyed once, and you know you are exiting any triggers, so it makes sense to hand-place clean-up code right before the Destroy.

The hooks aren't set in stone. There's no reason Unity couldn't provide an OnTriggerDestroyExit function. But it would slow the engine a teeny bit, even for people who never use it.

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 Bluestrike · Sep 24, 2013 at 06:57 PM 1
Share

ok thats what I needed to know so I am not going to report this as a bug.

$$anonymous$$y solution is to move my object to y -5000 what moves it out of view and get an OnTriggerExit followed with waitForSeconds (0.2) and then destroy the object.

avatar image
0

Answer by Rick74 · Sep 24, 2013 at 05:14 PM

From my experience, you probably want to use the function OnTriggerStay instead.

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerStay.html

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

Answer by felixmann · May 27, 2017 at 08:44 AM

Looks like this was fixed 2 years ago for Unity 3D (thanks Owen!).

For Unity 2D this was a bug that has been fixed in Unity 5.6.

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 tanoshimi · May 27, 2017 at 10:24 AM 0
Share

Argh! Thanks for pointing out - that's going to need some code rewrites when i upgrade my project to 5.6 then!

avatar image Owen-Reynolds · May 27, 2017 at 02:22 PM 1
Share

This was fixed 2 years ago, in 5.0.

That link is the 2D version. Unity added explicit 2D later (before that you made 2D by just having everything be flat with an orthographic camera.) The physics part was incomplete; they've tweaking/completing it gradually.

avatar image felixmann Owen-Reynolds · May 27, 2017 at 04:57 PM 0
Share

Oh my bad, I'm working in 2D. Thanks for pointing that out!

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

OnTriggerEnter Question 0 Answers

OnTriggerExit doesn't work when scaling collider 0 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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