Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by SomehowLuke · Jul 04, 2016 at 12:51 PM · nullreferenceexceptiondontdestroyonload

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Here is a link: https://issuetracker.unity3d.com/issues/build-dontdestroyonload-object-is-destroyed-if-scene-is-changed-while-its-inactive-and-then-that-object-is-activated

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

61 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges