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
1
Question by Terminas · Feb 06, 2012 at 05:58 AM · destroyevent

Script on a destroyed GameObject still accepting event callbacks?

I have a script attached to a GameObject which does the following:

  • Determines a target GameObject and stores its 'entity' script in currTarget

  • Subscribes to the onDeath event of currTarget

  • Attacks currTarget

  • Calls Destroy(gameObject) when dead.

Now, let's say I have three GameObjects with the 'entity' script attached to them, A, B, and C. A is an enemy of both B and C. A's current target is B, and both B and C target A.

A kills B, so B Destroy()s itself and fires its onDeath event (captured by A). Many frames later, C manages to kill A, so A fires its own onDeath event. What happens now is this event is captured by both C (still alive) and B (supposedly destroyed).

Strangely enough, this test if statement actually passes as true (and the Debug message is displayed):

 if (this == null)
         Debug.Log("I should be dead!");

...so how can something that is null execute in the first place?

Am I missing something here? It seems very strange that scripts should still be running if their parent GameObject has been destroyed.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tingham · Feb 06, 2012 at 06:02 AM

Check out: http://stackoverflow.com/questions/292820/how-to-correctly-unregister-an-event-handler

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
2

Answer by flamy · Feb 06, 2012 at 06:49 AM

In OnDestroy function unregister the even handler

for example :

_EventDelegate+=function;  //Registering 
_EventDelegate-=function;  //unregister..

this would avoid these confusions

Comment
Add comment · Show 2 · 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 illustir · Sep 03, 2015 at 02:39 PM 0
Share

But if you register an anonymous lambda? How do you unregister that?

avatar image $$anonymous$$ · Sep 03, 2015 at 02:59 PM 0
Share

If it's a one shot event you can write the unregister call last in the lambda but if you want to have it consume events till you want to kill it then you should probably do named functions ins$$anonymous$$d of lambdas.

Other than that there's also _EventDelegate = null but that's not good if you want to remove only one listener/lambda.

avatar image
0

Answer by $$anonymous$$ · Nov 23, 2013 at 12:50 AM

(y) to flamy

Also a good idea is to have functions for registering your object to all events you need and another for the opposite and call them at correct times.

Ie if you want to deactivate gameobjects but want them to continue listening to events in case they are reactivated then call the UnRegisterAllEventsINeed function upon deactivation so that the eventhandler doesn't try to call the function from the deactivated object any more and call RegisterAllEventsINeed inside OnEnable and accordingly UnRegisterAllEventsINeed inside OnDisable to automate the process.Anytime you deactivate them you also take them off the listener list and when you reenable them they get back on the listener list.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Input versus Event 1 Answer

OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers

How to emit particle from a collision? 2 Answers

Is it okay to mark well-known events as private? 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