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
0
Question by jakovd · Jan 27, 2015 at 12:14 PM · collision2ddestroy objectdocumentation

Documentation unclear about destroying the object

I would like to see if this problem make sense to others than me.

I think that the documentation for Object.Destroy is wrong when saying:

Actual object destruction is always delayed until after the current Update loop (...)

I happen to call Destroy in OnCollisionEnter2D event and object gets destroyed before Update is called.

 using UnityEngine;
 
 public class TestCollision : MonoBehaviour
 {
     private bool destroy;
     public void OnCollisionEnter2D(Collision2D coll)
     {
         Debug.Log(string.Format("{0} collided with {1}", name, coll.gameObject.name));
         destroy = true;
         Destroy(gameObject);
     }
     public void Update()
     {
         if (destroy)
         {
             //This does not get called, no Update after Destroy is called.
             //Gets destroyed before Update
             Debug.Log("Update gets called before destroying the object");
         }
     }
 }

Shown in a diagram from Execution Order of Event Functions

alt text

Should this be considered for submitting for a false documentation statement?

destroy-on-collision-enter.png (82.6 kB)
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
2

Answer by meat5000 · Jan 27, 2015 at 12:22 PM

The references become unregistered but I'm sure the actual destruction happens next frame.

I have observed the effects of this using Overlap Sphere. I deleted a GameObject and within the same frame launched a sweep of Overlap Sphere, with which the Destroyed object was detected.

I had to use DestroyImmediate to remedy my problem.

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
1

Answer by sniper43 · Jan 27, 2015 at 01:01 PM

I think it means if you destroy INSIDE the Update() function the object will remain until Update() finishes.

An Update loop is current if it's being executed. Before that it's the next Update loop. Just because it's the same class does not mean it's the same loop.

You need to adapt your code so that this can't happen, beacuse influencing when what happens in seperate unreferenced sciprts is to my knowledge not possible.

Try:

 using UnityEngine;
 public class TestCollision : MonoBehaviour
 {
     private bool destroy;
     public void OnCollisionEnter2D(Collision2D coll)
     {
         Debug.Log(string.Format("{0} collided with {1}", name, coll.gameObject.name));
         destroy = true;
     }
     public void Update()
     {
         if (destroy)
         {
             Destroy(gameObject);
             //This didn't get called because it wasn't inside of the current Update loop. It should still log.
             Debug.Log("Update gets called before destroying the object");
         }
     }
 }

And it should work.

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 jakovd · Jan 27, 2015 at 01:13 PM 0
Share

Documentation should at least mention when it will be destroyed if used in OnCollisionEnter2D. This way it is unclear why this happens: if you have two object colliding and first destroys itself on collision, the second won't get even OnCollisionEnter event because the first object is already gone. I would presume that if collision happened, both will have the event raised, no mater what happens to the other one.

avatar image sniper43 · Jan 27, 2015 at 03:54 PM 0
Share

I imagine that all OnCollisionEnter, Update, etc. are delegate functions. It might work that after each delegate there is a check to see if anything was destroyed. But yes, the API should expand on that.

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

21 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

Related Questions

Collision detected but destroy not working 2 Answers

Why does Destroy(this.gameObject) destroys a prefab that got nothing to do with the script 0 Answers

Why can't I destroy this object!? 3 Answers

Bouncing the ball off a collider and destroying it using trigger 0 Answers

OnTriggerEnter2D called twice - doing UFO tutorial 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