Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by LunaTrap · Apr 25, 2016 at 11:14 PM · c#performanceeventscgdelegates

Question about unsubscribing eventhandlers OnDisable and OnEnable

Hi! this 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 this make me ask, how heavy really is subscribing to events? i need to keep this into account, since my AI will spawn from my pool, and will get disable and enabled multiple times, and i was thinking, 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 think?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
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 thing 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
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 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
4

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 which 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
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 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.

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

Regarding Update() Performance 0 Answers

System or Unity Action? 1 Answer

How do Delegates and Events work? 1 Answer

Stack Overflow error on delegate call 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