DontDestroyOnLoad works in editor but not on android target

Hello there,
I am having problems with DontDestroy on load.

I have a GameObject called ActiveLoading. It basically gets activated for fading scenes out and in, for loading something from database. So it handles all loading things visually.

It contains following childs:

  • Textfield: LoadingText
  • Image: Animation
  • Background: Covers entire mobile screen Debug
  • Textfield: custom debug purpose

on the parent object I have a Script attached that does following:

void Awake()
        if (current != null && current != this)
            current = this;

To have access to that UI-Screen I call DontDestroyOnLoad(this.gameObject) and save it into a static variable.

When I call somewhere else, no matter what scene I am in:


it will activate itself. There is also 3 public variables:

public Image CharAnimation;
public Image BackgroundImage;
public Text LoadingText;

and I define them over the editor. (Drag child elements in there)

for now it is not important what is actually going on in that startLoading function. More important is the Debug.Log();

Debug.Log("Child Elements: " + BackgroundImage + " " + CharAnimation + " " + LoadingText);

When I use the editor I get always this result and it works great:

Child Elements: ScreenBG
(UnityEngine.UI.Image) Image
(UnityEngine.UI.Image) Loading Text

But on target (Android) it throws an error and does not go on (from Logfile):

(In Menu where its inited):

Child Elements: ScreenBG
(UnityEngine.UI.Image) Image
(UnityEngine.UI.Image) Loading Text

Next Screen GamePlay when trying to fade out to Menu again (fadein works):

Child Elements: null null null
NullReferenceException 07-04
14:23:04.062 22730 22745 I Unity :
(System.String startLoadingText,
Single alpha, Single fadeDuration,
Boolean showDetails) [0x0008d] in

Does anyone have an idea why it does not work on target but on editor?
Thank you for your help and ideas!

all best,

I got the same issue with Android devices. I tested and it occured on Unity version 5.4.0 and 5.5.0. In Editor, this issue occur randomly. Fortunately, I’ve found another simpler solution for this :

Call DontDestroyOnLoad() in START(), NOT in Awake() !!!

It worked for me with version 5.4 and 5.5, not tested on older version yet. Maybe It a Unity bug, I’ve not found an exact steps to reproduce this bug on Editor.

I managed the problem by moving the ActiveLoading.gameObject from Main Menu Scene to the new Scene with

SceneManager.MoveGameObjectToScene(ActiveLoadingController.current.gameObject, newScene);

before I manually Unload it. But I still dont get why I am even using DontDestroyOnLoad when it does destroy it on target (but not in editor)…

Best regards

We were facing the came issue. We called DoNotDestroyOnLoad() on the inactive game object (it set itself to disabled at the start) and then unloaded the scene. The marked game object was destroyed as a result.

Here is a link: Unity Issue Tracker - [Build][DontDestroyOnLoad] Object is destroyed if scene is changed while it's inactive and then that object is activated

I just found out that the objects get null when I do follwoing call:

SceneManager.SetActiveScene (SceneManager.GetSceneByName (GameStatics.SceneNames.Gameplay));

before that code the elements are set, and after that they are null. But I set DontDestroyOnLoad and in editor it works but on device not…