• 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
Question by hexagonius · Aug 11, 2015 at 12:48 PM · awakeinitializationexecution orderconstructorsonenable

OnEnable not called after all Awake and not all OnDisable before OnDestroy?

The main reason I ask t$$anonymous$$s is, that OnEnable and OnDisable seem to be perfect to toggle t$$anonymous$$ngs on and off, right? Like Event-Subscriptions. Subscribe in OnEnable and Unsubscribe in OnDisable, nice. In my case, that leads to a problem, because I execute code in the add section of one of those property events. Unfortunately, the Object it's operating on is not yet referenced, because Awake on the other Object has not been called. at that time. I recognised, that I get an alternating chain of Awake and OnEnable if I debug.Log those calls.

Since the Constructor is ran by Unity, why is there not one method that runs on all GameObjects first. T$$anonymous$$s way everyt$$anonymous$$ng else would be bulletproof.

Same goes for OnDisable. I CAN'T Unsubscribe from events here, because the object I was subscribed to might not exist anymore. OnDisable and OnDestroy are also alternating their calls... How am I supposed to use toggling a script on and off with that?

Comment

People who like this

0 Show 0
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

Answer by darthtelle · Aug 11, 2015 at 01:16 PM

Have you checked out t$$anonymous$$s Execution of Events article? Might shed some light on the actual order functions are called in the Unity lifetime. I've never seen or heard of the functions being called out of order, so you might need to rearrange some of your function calls. It's usually recommended that variables are initialised in Awake() and references set in Start(), but of course t$$anonymous$$s differs for different projects and different scenarios. You might need to delay grabbing references until the t$$anonymous$$ng you require is initialised. Even have the initialise function send off an event once it's up and running and THEN toggle t$$anonymous$$ngs when receiving that event? I'm afraid I'm short of other suggestions without actually looking at your project because it sounds pretty specific!

Comment

People who like this

0 Show 4 · 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 hexagonius · Aug 11, 2015 at 02:09 PM 0
Share

It's actually quite in general: You can nor rely on all Awake methods being called in OnEnable, nor can you rely on all OnDisable being called before OnDestroy e.g. the actual destruction of Objects. The latter gives me errors, because on stopping the game i want to cleanly unsubscribe from events in OnDisable, which are already composted at that point. When shall I do that then???

In my specific case, I cannot use script execution order, because it would need to change during gameplay.

The order I would need it in is: - All Awake - All OnEnable - All Disable - All OnDestroy

I mean for what reason would one use OnEnable if it's for local staff on that Object only??? It's just weird

Edit: This link shows OnDisable and OnDestroy on different execution steps... not happening. OnEnable is shown after Awake along with Start, which is executed after all Awake are called which doesn't apply for OnEnabled... pretty messy if you ask me

http://docs.unity3d.com/Manual/ExecutionOrder.htm

avatar image darthtelle · Aug 11, 2015 at 02:37 PM 0
Share

I was under the impression that all Awake(), OnEnable() etc calls are for their own object. Their own MonoBehaviour. So if their object is destroyed, their OnDisable and OnDestroy is called. It doesn't work on a global scale?? I'm afraid you've confused me now with your talk of "messy", but I guess I've not had the same issues with execution order as you have.

Have you tried putting the unsubscribing in a different function? So they get called regardless of order?

 private void Unsubscribe()
 {
     Foo.On_Event -= Bar;
 }
 
 private void OnDisable()
 {
     Unsubscribe();
 }
 
 private void OnDestroy()
 {
     Unsubscribe();
 }
avatar image hexagonius · Aug 11, 2015 at 04:49 PM 0
Share

I meant global in sense of when happens what with ALL objects. Like the fact that the first call to Start happens after all Awake calls are done. That's a necessary thing for local and remote Object initialisation. But it seems you can't use OnEnable/-Disable that way.

And just for curiosity, how does one more level of indirection help here?

Different appoach: if you destroy a gameobject during game which was subscribed to events and that object still exists, how do you unsubscribe if you can't use OnDisable or OnDestroy, because itwellfail if you quit the game instead of destroying the object?

avatar image darthtelle · Aug 12, 2015 at 07:04 AM 0
Share

I apologise for not being able to give you an answer. I was merely trying to help and was making different suggestions for you to try. I wish you the best of luck with finding a solution.

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

24 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

Related Questions

Execution Order of Scripts - Awake and OnEnable 1 Answer

Cached references set in awake aren't persisting 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Constructor "functionality" without Constructors? 2 Answers

Getting around deferred Awake call from SetActive 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