• 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 LunaTrap · Apr 25, 2016 at 11:14 PM · c#performanceeventscgdelegates

Question about unsubscribing eventhandlers OnDisable and OnEnable

Hi! t$$anonymous$$s is a pretty straightfroward question

I use events on various places of my AI scripts, and i have read that event subcription and unsubcription must be done in OnEnable and OnDisable respectively, wich works well enough but since im using pooling for my AI's OnEnable and OnDisable will be called more than 1 time, and sometimes, 10 enemies will get enabled at the same time, therefore making 10 calls to OnEnable.

All t$$anonymous$$s make me ask, how heavy really is subscribing to events? i need to keep t$$anonymous$$s into account, since my AI will spawn from my pool, and will get disable and enabled multiple times, and i was t$$anonymous$$nking, maybe instead of OnEnable and OnDisable i should use Awake and OnDestroy, since these will get called only once, so no matter how many times my Ai's get enabled and disabled, the event subscription will get called only once, but then comes out the question of memory leaks, what do you t$$anonymous$$nk?

In summary :

-Are OnEnable and OnDisable really the best place to subscribe and unsubscribe?

-Would Awake and OnDestroy also work?

-How performance and CG heavy is event subscription and unsubcription?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by RakshithAnand · Apr 26, 2016 at 11:31 AM

@LunaTrap Yes it is the best place. If you subscribe to an event in Awake, then the event handlers will still be called when the gameobject is disabled. Hence its better to unsubscribe it in OnDisable

Yes Awake and Destroy would still work but handlers are still executed if disabled as said above.

Events are fast and the t$$anonymous$$ng you need to worry about is, you have to unsubscribe at some point in time else events will hold a reference even if objects are destroyed.

Comment
FlightOfOne
TheJoluk

People who like this

2 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 LunaTrap · Apr 26, 2016 at 05:15 PM 0
Share

I see, then i guess i need to suck up the small hit on performance and memory allocations, thanks a lot

avatar image

Answer by ben-rasooli · Apr 26, 2016 at 01:11 PM

There is no rule where to unsubscribe your eventHandler, you should unsubscribe whenever you're done with it. It can be even in a simple function. If you don't have many objects in your pooling system w$$anonymous$$ch use the same handler, or if you know you're gonna subscribe to it again soon, then just leave it there stay on the object.

Comment
fafase
FlightOfOne
TR_Chris
DhiaSendi

People who like this

4 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 fafase · Apr 26, 2016 at 06:23 PM 1
Share

This is in my opinion a more appropriate answer. OnDestroy/OnDisable are only appropriate if they are the right moment to unregister. There are cases when you unregister in the listening method so that it happens only once. OnEnable is right if OnDIsable is used, or it will pile up listeners. It is all a matter of personal design in the end.

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

145 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 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 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 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 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

Distribute terrain in zones 3 Answers

Subscribers of Delegates? 2 Answers

How do Delegates and Events work? 1 Answer

Question on Events and Delegates 1 Answer

Unity Events, Listeners, Delegates, etc. 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