- Home /
DontDestroyOnLoad() fails in Player
I have this helper class that I use to store components that should stay alive even when changing scenes:
public static class Session {
/// <summary>Name of the game object that will carry the session state</summary>
public const string SessionGameObjectName = "Session";
/// <summary>Retrieves or create the game object holding the session state</summary>
/// <returns>The game object responsible for holding the session state</returns>
public static GameObject GetOrCreate() {
if(sessionGameObject == null) {
sessionGameObject = GameObject.Find(SessionGameObjectName);
if(sessionGameObject == null) {
Debug.Log("===== Session game object not found, creating a new one");
sessionGameObject = new GameObject(SessionGameObjectName);
}
GameObject.DontDestroyOnLoad(sessionGameObject);
}
return sessionGameObject;
}
/// <summary>Game object that is holding the session state</summary>
private static GameObject sessionGameObject;
}
In the Unity editor, it works flawlessly.
However, after compiling, in the player, when a new scene is loaded, GetOrCreate()
does not find the supposedly protected Session
game object and creates a new one each time.
Am I using DontDestroyOnLoad()
wrong or what's going on here?
Answer by Cygon4 · Aug 16, 2013 at 04:14 PM
It seems that I can solve my issue this way:
public class DontDestroyOnLoadCaller : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(this.gameObject);
}
void Start() {
DontDestroyOnLoad(this.gameObject);
}
}
Adding that component to the Session
gameo bject right before the return sessionGameObject;
line makes everything work as expected.
So it seems DontDestroyOnLoad()
may only be called from Awake()
or Start()
of a MonoBehaviour
in the same GameObject
for some reason.
No I'm calling $$anonymous$$e in a Coroutine called from a function that is called from Start. And my destroy on load applies it to an array of objects. So it is not limite to the object the script is on. And I doubt it is limited to the Start or Awake functions.
On the other hand, in your example, if the sessionGameObject
is never null, the DontDestroyOnLoad is never applied to it. So maybe it is already assigned when you call your GetorCreate function?
In my example sessionGameObject
is only ever assigned through the GetOrCreate()
method I posted, so if it's non-null, DontDestroyOnLoad()
will have been called on it.
I'll have to do some more testing to find out what exactly causes DontDestroyOnLoad()
to be ignored. $$anonymous$$aybe calling new GameObject()
and then DontDestroyOnLoad()
directly after is the culprit.
It's also interesting that it does work perfectly in the editor and only fails when I create a standalone build of my game.
Answer by Xtro · Aug 16, 2013 at 04:11 PM
Hey, where do you declare the sessionGameObject variable? I can't see it here. I think it must be a "static" variable in this class. Right ?
Yes, I forgot to copy & paste that line. Thanks for pointing it out :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613112342im_/https://answers.unity.com/themes/thub/images/avi.jpg)