Destroying Destroyed stuff

Really dumb question, but can’t seem to find the quick answer for it.

So I have a resource manager, and it is in charge of our game’s loading of asset bundles, instantiating them and keeping a list of them. In between scene loads I call a method to destroy all the instances of each bundle, bundles are kept in memory though for quick instantiation later, this makes it so the loading of a bundle only happens when we need it, and remains there until we forcible destroy its instances and unload the bundle itself. I know Unity destroys instances in between scenes as well, but assuming I don’t know who destroys all the instances first, what’s the risk associated to calling Destroy on an instance that has already had Destroy called on it? I am hoping unity will just ignore, but I’m not sure.

If you try to destroy something which doesn’t exist, you get a null reference exception. If there’s any chance you might try to refer to a non-existing object at any point, always do a check to see if that object exists before doing anything to it.

–Eric

Not necessarily, when Destroy is called on a game object, it’s not guaranteed to be removed immediately, so if I had multiple references to a game object that Destroy was called on, those references will still be pointing to valid memory. If they are pointers to that object, and that object really is destroyed at some point, the memory address would still be there and they would still be pointing to that location, but that location would be junk. To avoid the junk though, I know that C#(As well as Java and even AS3) internally will keep reference counters to anything pointing at an object, in which case if the object has been marked for garbage, it won’t actually remove it until all references are disconnected, this ensures you don’t have junk pointers like you would in C++(Here’s the gun, shoot yourself in the foot). So even if Unity destroyed a game object, and I have a reference to that same game object in my manager, that object(While removed from the scene graph) is still there and valid and not null, so what happens when I call Destroy on it again? Will Unity throw an exception or will it ignore the second Destroy call?

Umm… That doesn’t happen to any of my game objects…

Whenever I call Destroy(gameObject), I immediately have null pointers to it.

I believe Unity has some specific “hacks” wherein this happens. Whatever it is, when you call Destroy on something, it is (at least by the end of the frame) not only destroyed, but any pointers to any components of that object become null.

Sorry for late late response - I created a sample project just for testing this, then forgot to make any replies here. You guys were absolutely right, somehow Unity is setting any of my references both direct and indirect to an object I destroy to null - very cool(Since normally this is not the case)

Destroy() doesn’t throw null reference exceptions. Here is a simple test:

function Start () {
	var testObject : Texture2D = new Texture2D(24, 24);
	Destroy(testObject);
	yield;
	Debug.Log(testObject == null);
	Destroy(testObject);
}