- Home /
Instantiate OnDestroy
I have a couple of objects that spawn a particle effect, when they are destroyed. Everytime I start the game from within Unity and then stop it, each object I have not destroyed during runtime spawns his death-effect and these linger in my hierarchy. Is there a way to prevent this from happening?
Script: function OnDestroy() { Instantiate(object, position, rotation); }
The problem is that OnDestroy gets called when the objects get cleaned up when you go back to the editor! If you don't want this to happen, you will have to spawn your death effects another way.
Feared as much...luckily it is no big deal to pull the Instantiate out of OnDestroy and put it right before I destroy the object itself.
That sounds like you use ExecuteInEdit$$anonymous$$ode... Are you sure that you need the scripts functionality at edit time? You have to be careful with ExecuteInEdit$$anonymous$$ode, it can break your whole scene when used wrong. Only use it when you really need it and you know what you're doing...
No no..it is as syclamoth said. When I end playmode, all objects get cleared and call their OnDestroy function. I didn't even know about ExecuteInEdit$$anonymous$$ode until now :D
Answer by Tommynator · Sep 23, 2011 at 09:28 AM
Make use of the fact that on each MonoBehaviour OnApplicationQuit() is called before OnDestroy():
private bool isQuitting = false;
void OnApplicationQuit()
{
isQuitting = true;
}
void OnDestroy()
{
if (!isQuitting)
{
//your code
}
}
Cheers, Tommy
The diagram at the bottom here seems to suggest the opposite: http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
It shows OnDestroy
is before OnApplicationQuit
. Is it wrong?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
moving an object with swipe for ios? 3 Answers
Object reference not set to an instance of an object 1 Answer
Health Script 1 Answer
How to make GUI text disappear after a few seconds? 1 Answer