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
3
Question by sient · Sep 05, 2013 at 09:48 PM · execution orderorderingondestroyondisable

OnDisable/OnDestroy Execution Order

I have a number of objects, A through F for this example, and I am seeing some behavior that is confusing to me when exiting a level or stopping play mode. The OnDisable/OnDestroy execution order is as presented:

 A OnDisable
 B OnDisable
 C OnDisable
 A OnDestroy
 B OnDestroy
 C OnDestroy
 
 D OnDisable
 E OnDisable
 F OnDisable
 D OnDestroy
 E OnDestroy
 F OnDestroy

Please find attached an example via the Unity console.

This presents a problem because I am referencing other components in OnDisable; however, they are sometimes already destroyed (I could check to see if the reference is already destroyed and then not touch it, but that is an ugly solution for a problem which should not exist). The script execution order for all of these components is the same.

Is there a proper workaround for this behavior?

Thanks.

image.png (45.2 kB)
Comment
Add comment · Show 2
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 bubzy · Sep 05, 2013 at 10:07 PM 0
Share

how will you know if they are destroyed if you don't check? you can have a List with the items in and remove them when they are destroyed then you only check the ones that are left. but at some point you will need to check whats happening.

avatar image sient · Sep 05, 2013 at 10:47 PM 0
Share

I should have been more explicit in the question, but the "destroyed" reference exists for the duration of the entire level, but the components which access it do not exist for the entire level duration. So to the component's point of view, the reference is never destroyed.

Similar to how Awake() is used for setting up internal state and OnEnable() is used for dealing with dependencies between components, OnDisable() could be used for dealing with dependencies and OnDestroy() could be used for cleaning up internal state.

So to answer your question, the referenced object should never be destroyed at the time of access. However, with the current OnDisable/OnDestroy behavior at level end / player stop, the reference sometimes is.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Elvis083 · Mar 03, 2015 at 12:17 PM

You can write simple script for disable all scripts in OnApplicationQuit event. It will solve your problem with errors when exit play mode.

 using UnityEngine;
 using System.Collections;
 
 public class Deactivator : MonoBehaviour {
     void OnApplicationQuit() {
         MonoBehaviour[] scripts = Object.FindObjectsOfType<MonoBehaviour>();
         foreach (MonoBehaviour script in scripts) {
             script.enabled = false;
         }
     }
 }
 
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 eventropy · Apr 15, 2016 at 05:41 PM 0
Share

This doesn't work when changing scenes though

avatar image
-1

Answer by alenwesker · Apr 25, 2014 at 08:38 AM

I encounter the same problem. Not a nice solution, but just check every external variables, find out if they are null.

if(a != null) xxx;

if(b != null) xxx;

Something like what I do in C. Inelegant, but works.

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

20 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

Related Questions

Does OnDisable()/OnStart() get always called OnApplicationClose? 0 Answers

Isolate OnDisable from OnDestroy 2 Answers

Detect when GameObject has been deleted / removed from scene 10 Answers

Unsubscribe events in OnDisable causes NullReferenceException 2 Answers

OnDestroy called when object lost focus in editor 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