console recommends DestroyImmediate()?

I’ve always been under the impression that the use of DestroyImmediate has been discouraged, with favor going to Destroy. In fact, this is explicitly stated in the official scripting reference.

Invoking Destroy on a prefab (grouped collection of particle emitters) in Unity3f1, however, I’m greeted with an error each time:

So… I’m a little confused right now.

If Destroy isn’t allowed, then where would I unload instantiated prefabs, for example? (At the moment, they just continue to populate the scene hierarchy).

In this particular case I could just instantiate each of the emitters individually in the script, but the point is that Unity is now explicitly recommending DestroyImmediate.

The recommendation has actually been in Unity as long as I’ve been using Destroy in the Editor. (2008, I believe, or longer.)

You’ve been under the wrong impression. (Personally, I recommend hovering under a honey tree instead of the wrong impression.) Why would the function even be in the API if it didn’t have a use? :wink:

Nope.

Now, the thing is, is that “game code”, when we’re talking about Destroy, deals with objects instantiated in the Hierarchy, not assets. When you use a prefab from the Project pane, in a variable slot, you now have a reference to the asset. Destroying it is not “game code”. You need to use an instantiated prefab, from the Hierarchy, instead. Unity 3 makes this really easy. Just don’t click on the “Assets” panel:

390425--13428--$screen_shot_2010_09_20_at_90349_pm_118.png

So as it turns out, I’d accidentally invoked Destroy on the prefab reference itself, instead of the instantiated clone (whoops, typo!). Fixing that got rid of the error message.

As for your reply, now I’m really confused. Mind, I wasn’t saying that DestroyImmediate didn’t have any use at all, just that the docs appear to discourage its use except in special cases. I know I wasn’t getting this error in 2.6 (or even Unity Wii). And the portion of the docs you quoted specifically say that DestroyImmediate should only be used for editor code. Which again goes towards my standing impression that it’s discouraged in non-editor (game) code.

As for setting the variable reference to a prefab in the scene, doesn’t that ignore the advantage/point of instantiating a prefab that doesn’t/shouldn’t already exist in the scene (pfx for explosions, weapon projectiles, etc)?

(Mind, I’m not trying to be a twat, just honestly confused. I’m not sure if I’ve gotten some of my concepts muddled)

One of the most important reasons for having prefabs is that you can have more than one of it instantiated in the scene. What exactly do you want Destroy to do in that case?

This part I’m familiar with. In which case I think we might be misunderstanding each other. :stuck_out_tongue:

Right now I’ve got the script referencing the prefab selected from the Assets portion of the browser, since it’s not meant to exist at scene load. I’m then instantiating it as needed (explosion pfx).

My initial error message came out of my having typo’d my Destroy call (I’d mistakenly been trying to destroy the prefab itself, instead of the instantiated clone).

Okay, I get you. I can’t see your code, so I have no idea what your variables are. I didn’t know you were instantiating the prefab stored in your variable; I assumed you were just destroying it. You seem to understand what you need to understand.

Personally, I don’t really use Instantiate or Destroy. Better to just keep around as many as I’ll need, and enable/disable appropriately, to avoid garbage collection.

Cheers, and thanks :slight_smile:

Yeah, I can definitely see how that would be helpful in cases. For others, though, when you can’t know beforehand how many are going to be needed, instantiate and destroy work a treat (though I’ll definitely file your point for later on once I start hitting an optimisation cycle).

That’s good… if Destroy stopped working, it would ‘destroy’ a lot of my existing projects! :wink: