DontDestroyOnLoad works in editor but not on android target
Hello there, I am having problems with DontDestroy on load.
SETUP: 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)
{
Destroy(this.gameObject);
return;
}
else
{
current = this;
}
DontDestroyOnLoad(this.gameObject);
}
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:
ActiveLoadingController.current.startLoading();
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 (UnityEngine.UI.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 (UnityEngine.UI.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 :
at ActiveLoadingController.startLoading (System.String startLoadingText, Single alpha, Single fadeDuration, Boolean showDetails) [0x0008d] in 02_MainMenu\ActiveLoadingController.cs:63
Does anyone have an idea why it does not work on target but on editor? Thank you for your help and ideas!
all best, Markus
Answer by SomehowLuke · Jul 04, 2016 at 02:35 PM
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
Answer by ThinhHB · Dec 28, 2016 at 07:42 AM
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.
Answer by vstefaniuk · May 05, 2017 at 03:52 PM
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.
Answer by SomehowLuke · Jul 04, 2016 at 01:53 PM
I just found out that the objects get null when I do follwoing call:
SceneManager.SetActiveScene (SceneManager.GetSceneByName (GameStatics.SceneNames.Gameplay));
SceneManager.UnloadScene(GameStatics.SceneNames.MainMenu);
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....
Your answer
Follow this Question
Related Questions
Why is the value of my variable being reset when changing scenes? 1 Answer
DontDestroyOnLoad not working 4 Answers
DontDestroyOnLoad duplication despite having code that should stop it 3 Answers
Map select from menu trouble with DontDestroyOnLoad() 0 Answers
How to fix (NullReferenceException: Object reference not set to an instance of an object) in my case 0 Answers