• 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 Ziron999 · Aug 30, 2015 at 10:35 AM · destroyontriggerexit

OnTriggerExit not called on Destroy()

This question has been asked before however the difference is I will have a procedural generated system and the solutions of using lateupdate won't cut it. there needs to be a proper trigger type way to do this in some way here is my code:

 void OnTriggerEnter(Collider other)
     {
         //better performance but no way to check when object is destroyed...
         //if (other.gameObject.layer == 8 && other.transform.parent != transform.parent)
         //{
             //other.GetComponent<MeshRenderer>().enabled = false;
             //mrenderer.enabled = false;
         //}
 }

     void OnTriggerExit(Collider other)
     {
         //better performance but no way to check when object is destroyed...
         //if (other.gameObject.layer == 8 && other.transform.parent != transform.parent)
         //{
             //other.GetComponent<MeshRenderer>().enabled = true;
             //mrenderer.enabled = true;
         //}
 }

so when destroying the other object ontriggerexit is not called so I cannot get mrenderer.enabled to = true..this same script is on 6 sides of 1 cube. This is why a lateupdate solution is not a possibility it would slow down the system way to much with massive amounts of cubes. This is part of my dynamic memory management...

for more information this is an example of how it would be with update cluttered up from just this 1 detection...as you can see this simply just isn't a solution...

 MeshRenderer mrenderer;
 bool ftrigger = false;
 bool ttrigger = false;
 bool ltrigger = false;
 bool rtrigger = false;
 bool btrigger = false;
 bool batrigger = false;
 Collider otherf;
 Collider othert;
 Collider otherl;
 Collider otherr;
 Collider otherb;
 Collider otherba;

 void Start()
 {
     mrenderer = GetComponent<MeshRenderer>();
 }

 void LateUpdate()
 {
     if (ftrigger && !otherf)
         mrenderer.enabled = true;
     else if (ttrigger && !othert)
         mrenderer.enabled = true;
     else if (ltrigger && !otherl)
         mrenderer.enabled = true;
     else if (rtrigger && !otherr)
         mrenderer.enabled = true;
     else if (btrigger && !otherb)
         mrenderer.enabled = true;
     else if (batrigger && !otherba)
         mrenderer.enabled = true;
 }

 void OnTriggerEnter(Collider other)
 {
     //better performance but no way to check when object is destroyed...
     if (other.gameObject.layer == 8 && other.transform.parent != transform.parent)
     {
         other.GetComponent<MeshRenderer>().enabled = false;
         mrenderer.enabled = false;
     }
     if (other.gameObject.name == "Front" && other.transform.parent != transform.parent)
     {
         ftrigger = true;
         otherf = other;
     }
     else if (other.gameObject.name == "Top" && other.transform.parent != transform.parent)
     {
         ttrigger = true;
         othert = other;
     }
     else if (other.gameObject.name == "Left" && other.transform.parent != transform.parent)
     {
         ltrigger = true;
         otherl = other;
     }
     else if (other.gameObject.name == "Right" && other.transform.parent != transform.parent)
     {
         rtrigger = true;
         otherr = other;
     }
     else if (other.gameObject.name == "Bottom" && other.transform.parent != transform.parent)
     {
         btrigger = true;
         otherb = other;
     }
     else if (other.gameObject.name == "Back" && other.transform.parent != transform.parent)
     {
         batrigger = true;
         otherba = other;
     }
 }
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

1 Reply

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

Answer by Ziron999 · Aug 30, 2015 at 11:09 AM

I have figured out a way around this by completely changing the way the destroy is being called. When destroying the object just move it first and use an ienumerator so onexit DOES trigger!

 IEnumerator DestroyObject()
     {
         transform.parent.position = new Vector3(0,10000,0);
         yield return new WaitForSeconds(0.2f);
         Globals.isRightMouseDown = false;
         Destroy(transform.parent.gameObject);
     }
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 Scribe · Aug 30, 2015 at 11:41 AM 0
Share

I was going to suggest something similar, note that there is a yield method called WaitForFixedUpdate that might be useful to you.

Here's another (heavier I would guess) solution using extension methods:

To attach to your object:

 List<Collider> inColliders = new List<Collider>();
 
 void Update(){
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)){
         StartCoroutine(gameObject.Destroy(value => {
             for(int i = inColliders.Count-1; i > -1; i--)
                 OnTriggerExit(inColliders[i]);
         }));
     }
 }
 
 void OnTriggerEnter(Collider other){
     inColliders.Add(other);
     Debug.Log("enter");
 }
 
 void OnTriggerExit(Collider other){
     inColliders.Remove(other);
     Debug.Log("exit");
 }

Static extension methods class:

 public static class Extension$$anonymous$$ethods {
 
     public static IEnumerator Destroy(this UnityEngine.Object obj, System.Action<bool> result, float timer = 0){
         yield return new WaitForSeconds(timer);
         result(true);
         UnityEngine.Object.Destroy(obj);
     }
 }


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

26 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

OnTriggerEnter2D called twice - doing UFO tutorial 1 Answer

walk through an object once 0 Answers

How do I use Destroy(gameObject) on the instance without destroying itself? 2 Answers

OnTriggerExit not called on Destroy() 5 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