- Home /
is it OK to use DestroyImmediate in game?
Hi!
Right now i'm using DestroyImmediate to destroy duplicates of a game controller (which is a singleton) that has been set to DontDestroyOnLoad(). The reason why i need DestroyImmediate is that otherwise the Awake() and Start() functions of the duplicate scripts will run, which in my case is very time consuming. The reason for having duplicates of the game controller is that i otherwise would have to run the game from the start scene everytime just to test a certain level.
According to the reference it's not recommended though, but is it still valid?
The main reason DestroyImmediate is recommended against is because it can destroy assets. Call it on a prefab and that prefab will be gone forever. Use with caution.
That does sound kinda scary. $$anonymous$$y gamecontroller actually is a prefab, but it doesn't get removed forever or anything like that. So calling it on a gameobject which in turn is a prefab is not dangerous i guess?
Fine to call on a instance in the scene. Not cool to call on a prefab in the assets folder.
DestroyImmediate was originally designed for use in editor scripts, that can modify and remove assets. However you can use it in game, as indicated below.
it can destroy assets
Id totally forgotten about that. After learning it I remember drea$$anonymous$$g up a anti-hacker/anti-cracking system based on breaking the game install :D~
Answer by Bunny83 · Aug 03, 2014 at 11:49 PM
It is still valid and in your case i would recommend a different approach ;)
When using a start scene, it's better to use a simple script in each scene that checks if your singleton is there. If not it simply calls Application.LoadLevel. If your starting scene only contains the singletons, you can even do a Application.LoadLevelAdditive. It's in general not a good idea to have duplicates of a singleton. You can also create a dedicated scene for the singleton initialization. That could even be used in the access method ( property of the singleton. So when you want to access the singleton and it isn't there, you simply load your singleton scene additively.
I've used the access method before. Psuedo code is something like this
private static GameObject _instance
public static GameObject instance {
public get {
if(_instance){
return _instance;
}
_instance = Instantiate (prefab);
return _instance;
}
}
Alright, thanks for the reply!:) The additive solution seems really neat. Think I'll try that one ins$$anonymous$$d. Thanks.
Answer by meat5000 · Aug 03, 2014 at 11:35 PM
I use it. In my case I need instant destroy to avoid nullrefs.
http://answers.unity3d.com/questions/522400/destroygameobject-and-colliders.html
Answer by khuramtycoon · Aug 23, 2016 at 06:53 AM
hi sir i m new in unity i use destroyimmediate function my object is destroy how i come back it ?? help me please
Please don't ask questions using the "Answer" button. Use the "Ask a question" button for that. As previously stated, DestroyImmediate destroys assets permanently and immediately - they can't be recovered. https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html
Answer by _eternal · Sep 07, 2018 at 03:23 PM
Late answer for the people who might come across this while searching like I did:
If you don't want to mess with your scenes or level loading code, just do something like this.
void Awake()
{
if (currentInstance)
{
Destroy(gameObject);
gameObject.SetActive(false);
return;
}
else
{
DontDestroyOnLoad(gameObject);
currentInstance = this;
}
}
Destroy
completes at the end of the current frame, but if you just disable the object and stop execution of Awake
, you can avoid some of the drawbacks without risking DestroyImmediate
. My issue was that other objects were finding the duplicate via FindObjectOfType
, so setting it to inactive worked for me.