Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
6
Question by ManuelAnimatoon · Jul 27, 2015 at 09:37 PM · destroysingletonexecution order

Order of destroy objects and OnDestroy event

Hi.

I'm working on a game where some singletons are present between scenes. However, other singletons are different in each scene so I don't use DontDestroyOnLoad method for these.

The problem I found is that the game object that contains the singleton is destroyed in an arbitrary order (checked by debug logs in the console). I can change the script execution order in the project settings but this seems not to work with OnDestroy and OnDisable events.

alt text alt text

The only thing I need is my singleton to be destroyed the last one. Is there a way to control the order of destroyed objects when a scene is loaded?

Thanks in advance!

orderunity.png (34.8 kB)
orderunity2.png (13.8 kB)
Comment
Add comment · Show 9
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 eventropy · Apr 15, 2016 at 06:36 PM 0
Share

I also can't seem to find a way to control destruction order. In my tests, "Script Execution Order" doesn't seem to apply to OnDisable and OnDestroy events. This is also consistent with the documentation which only mentions Awake, OnEnable, and Update.

So as far as I can tell, there is no way to control this. I would love to be proven wrong here though

avatar image Injourn · Apr 15, 2016 at 07:38 PM 0
Share

Oddly enough one of the things that seem to deter$$anonymous$$e the order in onDestroy and onDisable is the size of the string ("OnDestroy last" has 13 letters and "OnDestroy first" has 14). maybe its the size that deter$$anonymous$$es execution?

avatar image eventropy Injourn · Apr 15, 2016 at 07:44 PM 0
Share

@Injourn I think this is just a coincidence

avatar image Injourn eventropy · Apr 15, 2016 at 07:56 PM 0
Share

It was a coincidence that happened 6 times in a row, btw please stop necroposting.

Show more comments
Show more comments
avatar image eventropy Injourn · Apr 15, 2016 at 08:19 PM 1
Share

Agree to disagree then. Even if you don't have an answer, it can still help to provide more information on the problem. And it seems pointless to just make duplicate questions like you suggest, when you can just add more information to an existing one. This is how stack overflow works and for good reason I think

avatar image Dave-Carlile eventropy · Apr 15, 2016 at 08:38 PM 1
Share

Then you need to use comments, not answers. Answers are for answering the question only.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ShawnFeatherly · Jul 07, 2017 at 03:55 AM

Discovered the same issue. It's a bummer OnDestroy doesn't respect script execution order.

Couldn't find a way to set the order. Instead, discovered a way to deal with not knowing the order. I ended up creating a public static bool called WasDestroyed inside of the singleton class to track OnDestroy. WasDestroyed's value is set when the singleton calls its OnDestroy. Then I could check WasDestroyed in other scripts OnDestroy to see if I should be accessing the instance or not.

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 Dizy · Feb 14, 2018 at 11:01 AM

I encountered the same problem and if someone came across this topic I recommend to use a scene and to put your singletons inside, a scene manager.

Using SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene

Reference : Multi-Scene editing

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

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

8 People are following this question.

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

execution order of Destroy and Physisc.Raycast 1 Answer

Unregister Singleton Events before OnDestroy is called 1 Answer

Using Singleton but getting a null reference 2 Answers

Trouble with DestroyOnLoad,Game won't start after player dies once 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