- Home /
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. :*|
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.
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.
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.
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.
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.
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.
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.
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.
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?)
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
Follow this Question
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