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 Herman-Tulleken · Aug 12, 2010 at 11:58 AM · destroyupdate

How do I prevent Updates in scripts to be called once an object has been destroyed?

I have two scripts (X and Y) attached to a missile object. The Y script destroys the object once it collides with another object. But apparently, the Update of script X still gets called (I assume it happens only for one frame, if I read the Destroy documentation correctly). This gives me this exception:

MissingReferenceException: The object of type 'X' has been destroyed but you are still trying to access it.

In script X, I have tried to put in this line:

if(gameObject == null)
{
  return;
}

But it still gives me the same error (at the very point where I do the check).

I have also tried to do a DestroyImmediate, but then I get this exception when the object collides:

Destroying object immediately is not permitted during physics trigger and contact callbacks. You must use Destroy instead.

I can see how I can use a boolean (set in Y, tested in X) to get the behaviour I want but this seems so messy. In particular, I will have to add this check in every script that I attach to the missile...and that seems really silly.

Am I missing something? Is there a proper way to get objects destroyed and prevent any updates in attached scripts?

Edit: The actual update method was in fact not called. The code that was called was a regularly scheduled event (and not scheduled through Invoke, but our own scheduler). The schedule was not canceled when the object was destroyed, so the code was hapilly called by the scheduler. :*|

Comment
Add comment · Show 5
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 Wolfram · Aug 12, 2010 at 01:43 PM 1
Share

The sentence "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering." means, that the object will be destructed only AFTER all Update() functions have been completed.

avatar image Herman-Tulleken · Aug 12, 2010 at 01:59 PM 0
Share

Wolfram, but should the Update not just work then, ins$$anonymous$$d of throwing the exception? It seems it goes into some interim state where Update gets called, but does not work.

avatar image yoyo · Mar 16, 2011 at 12:21 AM 1
Share

I've got a similar problem -- but it's via OnDisable, not Update. $$anonymous$$y object owns some other objects which it wants to disable when it is disabled itself. However if everything is being destroyed then OnDisable is called, but the other objects may already have been destroyed -- in which case they aren't null, but accessing them throws the exception.

avatar image yoyo · Mar 16, 2011 at 12:21 AM 1
Share

And I found my solution -- just compare the other object with null. Unity overloads the comparison operator, so (obj == null) is true if obj is in process of being destroyed.

avatar image Herman-Tulleken · Mar 18, 2011 at 06:28 AM 0
Share

@yoyo - Thanks, that is useful information.

1 Reply

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

Answer by Wolfram · Aug 12, 2010 at 01:34 PM

It's the other way round. The error message means, that your script Y (or some other script) is still accessing object X or its components/scripts, so you need to make sure that anything accessing (parts of) obejct X stops doing that once X is destroyed.

The message does not come from object X, which you can verify by adding a print-statement in its Update() function (which will no longer be called).

EDIT: Have you tried double-clicking the "MissingReferenceException" error? It should bring you to the position in your code where the problem occurs.

Comment
Add comment · Show 5 · 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 Herman-Tulleken · Aug 12, 2010 at 01:47 PM 0
Share

Yup...it takes me right to the place in script X where I try to access gameObject... but let me do the test you suggest as well.

avatar image Wolfram · Aug 12, 2010 at 02:24 PM 1
Share

I cannot reproduce the behaviour you describe, it even works if you let script X destroy the object itself is attached to. For debugging, you could use function OnDisable(){print("I am being destroyed"); maybe that helps.

avatar image Wolfram · Aug 12, 2010 at 02:28 PM 1
Share

It might help if you edit your question to include the complete Update() function of script X, and also tell us the line the double click brings you to.

avatar image Herman-Tulleken · Aug 13, 2010 at 09:07 AM 0
Share

Wolfram, thanks for all your help. Your suggestions lead me to the solution of the problem, which was not even related to Update. (I edited my question to reflect this). I had a dummy Update function that was not called through the usual mechanism, but as a scheduled task that was executed every few frames, and I completely forgot this. So all I had to do was to unschedule the task. I feel a bit stupid for sending you (and others) on a wild-goose chase.

(PS - since my question is in fact "wrong", is there some way I can edit this to still be useful to others? And mark an answer?)

avatar image Wolfram · Aug 16, 2010 at 05:52 PM 0
Share

I guess your edit to your question explains it just nicely, and the rest of this page might still be useful to others, too.

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

No one has followed this question yet.

Related Questions

execution order of Destroy and Physisc.Raycast 1 Answer

How to destroy in an amount of time 1 Answer

How to destroy a gameobject by GameObject.Find 1 Answer

Is there a way to check how many objects are destroyed on the frame? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 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