- Home /
undoing DontDestroyOnLoad without immediately destroying the object
After calling DontDestroyOnLoad on a game object that we want to pass between levels. Eventually we want to finally destroy it... say to reset the game to the initial state.
Is there a way of undoing DontDestroyOnLoad without immediately destroying the object..
the command: Destroy(this.gameObject); will cause issues if other game objects that are in the same scene still depend on it. I just want it to get destroyed with all of the other game objects on the next level load.
I to wish you could write something like DontDestroyOnLoad (go, true); The boolean would say wether it is destroyed or not on load.
Answer by Mike 3 · May 26, 2010 at 11:26 PM
No way to do it automagically no
best way i can think of doing it is this:
var destroyOnLoad = false;
function OnLevelWasLoaded(level : int) { if (destroyOnLoad) Destroy(gameObject); }
that way you get your object destroyed before the first update in the new level
Due to event execution ordering the above code was giving me runtime threading errors.
I think the issue is that Destroy does not take action immediately and there is some overlap between its call, other event phases of the event loop, and its action.
The run-time error goes away if I do replace Destroy with DestroyImmediate. (however there could be still un-reported errors)... thats it for now.
Answer by Eagle32 · Jun 27, 2010 at 12:06 PM
If you're destroying it and it's then being recreated for the level you've just loaded to (I'm assuming this from your comment about destroy and destroyimmediate) why don't you make a reset method for the object that puts it back in its initial state.
That way you could just replace the destruction with a reset and carry on.
Answer by MikeHergaarden · Jun 14, 2017 at 08:18 PM
@badneighbour To undo a DontDestroyOnLoad, simply make a new GameObject and set it as parent of the object that was previously DontDestroyOnLoad.
GameObject bla= ...something;
DontDestroyOnLoad(bla);
GameObject newGO = new GameObject();
bla.transform.parent = newGO.transform; // NO longer DontDestroyOnLoad();
For convenience, I used this concept in an extension method class so I can remove any object from DontDestroyOnLoad by calling a method on its gameObject:
using UnityEngine;
public static class Extension$$anonymous$$ethods {
public static void RemoveFromDontDestroyOnLoad(this GameObject go)
{
GameObject newGO = new GameObject();
go.transform.parent = newGO.transform; // NO longer DontDestroyOnLoad();
}
}
so with this, you can call it on any GameObject like this:
gameObject.RemoveFromDontDestroyOnLoad();
Answer by Alverik · Aug 28, 2018 at 02:09 AM
Like I said in another post: DontDestroyOnLoad() simply moves an object to the special Scene "DontDestroyOnLoad", so all you need to do is move it back to the active scene:
SceneManager.MoveGameObjectToScene(TargetGo, SceneManager.GetActiveScene());
One extra note, TargetGo
should be the root of the hierarchy by itself, ie un-parented from any objects, like: TargetGo.transform.SetParent(null);
Your answer
Follow this Question
Related Questions
Method execution order after LoadLevel()? 1 Answer
Spawn carachters on locations and destroy the manager after level load 1 Answer
How to begin animated skybox at same point in sequence as when the scene ended? 0 Answers
DontDestroyOnLoad but script being excuted multiple times 1 Answer
how can i make my character to be on another scene? 1 Answer