• 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 beppim · Nov 30, 2015 at 03:58 PM · ondestroy

Order of OnDestroy()

Is it possible to determine the exact order in which objects are destroyed?

Or, in other words: at a scene closure, is it possible to know and determine if a OnDestroy() event will be called before or after the destruction of other objects in the scene?

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
0

Answer by ARKMs · Nov 30, 2015 at 08:34 PM

Yeah, with OnDestroy you can check with Find or save a reference of the GameObject to verify, to know if was already destroyed or still there the other GameObject. Something like:

 void OnDestroy()
  {
     if(SomeGameObject != null)
     {
         //is still alive
     }
     else
     {
         //was already destroyed
     }
 }

Now, if you want to check the order, put the Inspector in Debug mode and the "Instance ID" of transform with lower value with call first (This is my teory)

Comment
Add comment · Show 5 · 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 beppim · Dec 02, 2015 at 09:22 AM 0
Share

I will try that but then it would remain the doubt: will it always be the same order, after changing code, Unity version or platform?

avatar image Bonfire-Boy beppim · Dec 02, 2015 at 09:53 AM 0
Share

You're right, this doesn't answer your question about whether you can know what the order is going to be in advance.

I don't know what the answer to that question is. But I'm curious as to why you want to know. If it matters what the order is then I$$anonymous$$HO you probably want to rethink your design. Ideally, it shouldn't matter what order things get destroyed at scene closure (they're being destroyed anyway). If you've got some complicated cleaning up to do on scene closure, you're better off taking control of it yourself (ie write your own cleanup function that gets called before switching to the next scene).

avatar image beppim Bonfire-Boy · Dec 02, 2015 at 11:00 AM 0
Share

When a scene gets closed the user's configuration is triggered to be saved with a OnDestroy(), and this can lead to a sychronization to a remote server, hence to delegate this to another object, that must not be destroyed. Of course there might be a hundred of possible solutions to this "problem", for example check OnDestroy() in all objects or manually execute the sync before actually loading the new scene, but that problem made me curious and - btw - I think that it would be even more logical and clean to set the sync as automatically triggered by the dostroy, ins$$anonymous$$d of manually insert it in every button that could lead to a new scene load. :-)

Show more comments

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

32 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

Related Questions

OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers

How to destroy Scriptable Objects on Reimport 1 Answer

When I Multi-select gameobjects and press 'delete' 'OnDestroy' func is not called? 0 Answers

switch weapon onDestroy 1 Answer

Some objects were not cleaned up when closing the scene 2 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