- Home /
A way to check if an object has DontDestroyOnLoad activated
Is there a way to check if a gameobject (or its parent) has been set to DontDestroyOnLoad?
A good question that I would love to know the answer to...
Answer by Simon Says · Apr 07, 2013 at 01:49 AM
Assuming that there's no official way to query the property, I've come up with a workaround:
Created a simple MonoBehaviour script that calls Object.DontDestroyOnLoad(gameObject) in Awake(), and named it DontDestroyOnLoad.
function Awake() { DontDestroyOnLoad(gameObject); }
Attached the script to all objects I don't want to be destroyed, instead of doing the call from another component.
This way it's modular and I can see and modify the property in the editor. And more importantly, I can query it like this:
var obj: GameObject;
if (obj.GetComponent.<DontDestroyOnLoad>()) {
print("The object is kept between levels.");
}
Answer by Pacosmico · Dec 04, 2018 at 12:14 AM
unity has "gameObject.scene" for a while now
you can go all like
if (gameObject.scene.buildIndex == -1) //dontdestroy activated
nowdays
According to the docs, -1 means it was loaded by an asset bundle.
https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene-buildIndex.html
Are we positive that this works as expected?
gameObject.scene.buildIndex
will return -1 for addressable, assetbundle in additional to DontDesroyOnLoad scene. Check gameObject.scene.name == "DontDestroyOnLoad"
instead.
Answer by mateiasu · Jun 26, 2013 at 01:55 PM
We worked around this function call with HideFlags. Worked for us. I guess HideFlags.DontSave is almost the same as DontDestroyOnLoad().
gameObject.hideFlags = HideFlags.DontSave;
if (!((gameObject.hideFlags & HideFlags.DontSave) == HideFlags.DontSave)) {
// You may be destroyed.
}
DontSave means that the object is not saved to the scene. Further if you load another scene this object does not get destroyed, because it is not part of the scene. Just in case you're wondering why 'dont save' even you want to 'dont destroy' it.
Ouh and you could also display a enum popup in the inspector to define what flag should be assigned. More common approach?!
From the documentation:
// Instantiates a Plane everytime the game starts and never destroys it
// even if you stop your game preview
// NOTE: Watch out, this can cause LEA$$anonymous$$S
function Start() {
var notDestructable : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
notDestructable.hideFlags = HideFlags.DontSave;
}
It doesn't look exactly safe, does it? It appears to be meant for editor rather than for general usage.
As long as you just check for the flag without assigning it, it shouldn't be any problem.
Answer by crekri · Feb 21 at 04:15 AM
You can now use gameObject.scene.name == "DontDestroyOnLoad"
to check if a scene is flagged with DontDestroyOnLoad.
Note that the above answer of checking gameObject.scene.buildIndex
will return -1 for addressable, assetbundle in additional to DontDesroyOnLoad scene, and it likely isn't what you want.
Your answer
Follow this Question
Related Questions
donotdestroy for hud - but hud appears on main menu :( 1 Answer
DontDestroyOnLoad issue 1 Answer
Can you undo "dont destroy on load"? 1 Answer
dontdestroyonload object references 0 Answers
How to check scene number and use it in an If statement? 3 Answers