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 douglasg14b · Mar 30, 2015 at 01:10 AM · colliderondisable

How to manually call/fire OnTriggerExit() for colliders a trigger is inside?

I'm trying to detect when objects have left a collider when they are destroyed/disabled, so that I can stop attacking that object, as well as removing it from a collection it is stored in.

The documentation says "OnTriggerExit is called when the Collider other has stopped touching the trigger.", understandably confusing when thinking about a disabled object. I see threads ranging back 4 years asking about this issue. It seems the acceptable workaround is to move your object up a few thousand world units, this probably works, but it seems like a hacky solution to something that should be more elegant.

Can I somehow call OnTriggerExit from the collider(s) the object is currently in when it is disabled? Or a way to get OnTriggerExit to be called automatically when an object is disabled?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by sotirosn · Feb 07, 2016 at 01:44 AM

I think it is ridiculous that the OnTriggerExit is not called when the component is disabled. It should be called right before the object is disabled so that it can be properly handled by other objects that need to know what the heck they are touching. So basically it should work like this.

gameObject.SetActive(false); -> OnTriggerExit() { ... } -> OnDisable() { ... } -> object is now disabled

I don't understand why this is so hard. The best solution really is to move the freaken object out to the edge of space and then disable it.

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 sumeeton · Mar 30, 2015 at 05:00 AM

When you destroy the object or disable the object, the components on the object won't work which includes the script components too. So you'll have to make sure that the object stays alive for the collision trigger.

One way is to disable the renderer, so the scripts and colliders are still working but the object won't be displayed. You can destroy the object when you no longer need it. Like OnTriggerExit.

Another way is to instantiate another empty object with the same colliders and script components at the place of the object you destroyed.

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 douglasg14b · Mar 30, 2015 at 06:05 AM 0
Share

The object that is to be destroyed/disabled (object pooling) is no longer needed at the time it is to be destroyed/disabled. However the objects owning the colliders keep record of what come sin and goes out, and they are unable to record the object leaving if it is destroyed while within the bounds of the collider.

avatar image sumeeton · Mar 30, 2015 at 06:17 AM 0
Share

Yes, that was my answer.

Use two variables: bool insideCollider = false; bool destroySelf = false;

 OnTriggerEnter (Collider col)
 {
     insideCollider = true;
 }
 
 OnTriggerExit (Collider col)
 {
    // do your stuff here

    if (destroySelf)
    {
        // Destroy the gameobject here
    }
 
    insideCollider = false;
 }
 
 void DestroyGameObject ()
 {
    if (insideCollider)
    {
        // disable the renderer
        destroySelf = true;
    }
    else
    {
        // destroy the gameobject here
    }
 }


Ins$$anonymous$$d of destroying the object, call the function DestroyGameObject(). This way if it's inside the collider, it will only disable the renderer and when game object leaves the collider it will destory itself.

avatar image sumeeton · Mar 30, 2015 at 06:23 AM 0
Share

This might cause the OnTriggerExit to be not called on the other object since the gameobject is destroyed. So make the destroySelf property public and do not destroy the object in OnTriggerExit in this script. But check the destroySelf property in the colliders and then destroy the object.

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

22 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

Related Questions

Internal collisions 1 Answer

Are there any way to show an objects collider outline through scripting? 3 Answers

Get collision without attaching a script to every game object 1 Answer

Need some help an object collider that stops a timer then displays it on another scene 1 Answer

Determine which collider was clicked 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