Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by aljndrrr · Apr 20, 2013 at 01:04 AM · dontdestroyonload

DontDestroyOnLoad not working

I have the following:

 public class Singleton : MonoBehaviour
 {
     private static Singleton _instance = null;
     public static Singleton Instance
     {
         get { return _instance; }
     }
 
     void Awake()
     {
         if (_instance != null && _instance != this)
         {
             Destroy(gameObject);
             return;
         }
         
         _instance = this;
         
         DontDestroyOnLoad(gameObject);
     }
 }

I add the component to an Object (Transitions) expecting it will last for the next load, then when I load the next scene, my object is destroyed.

The GO Transition has another script attached and it's children have another script attached.

Any ideas on why DontDestroyOnLoad is not working?

Comment
Add comment · Show 3
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 whydoidoit · Apr 20, 2013 at 01:07 AM 0
Share

Are you sure there's no other code that could destroy it. As posted that code should work.

avatar image Owen-Reynolds · Apr 20, 2013 at 02:00 AM 0
Share

Is the script also on some other object (which would "claim" _instance?) Try printing Singleton.Instance.transform.name;

avatar image iaanus · Apr 22, 2013 at 10:15 AM 0
Share

I use the same pattern in a project of $$anonymous$$e. It works for me. There must be something else that interferes.

4 Replies

· Add your reply
  • Sort: 
avatar image
42

Answer by sotirosn · May 14, 2013 at 05:23 PM

I was having the same issue as you and then I realized that my singleton was not a root GameObject and its parent was being destroyed, thus destroying it as a child..

To fix this instead did: DontDestroyOnLoad(transform.root.gameObject);

which then introduced all kinds of problems with duplicate root objects, which I then had to merge all of the non-singleton objects together into under a single singleton root. Kind of a pain, but in the end it was worth it for a nested singleton.

Comment
Add comment · Show 3 · 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 Svarr · Jan 06, 2016 at 03:22 AM 0
Share

But what if the GameObject is a root object?

avatar image horoxix · Feb 03, 2019 at 05:39 AM 0
Share

Thank you ! Solved my problem.

avatar image MaxLohMusic · Mar 28 at 08:43 AM 0
Share

Wow... all I needed to do was move the object to be a root object (not nested in something else) and it worked as expected. It's pretty incredible how this isn't even mentioned in the documentation. DontDestroyOnLoad does NOT work unless the object in question is not under any other object.

avatar image
2

Answer by guybrush.threepwood · Mar 12, 2015 at 06:08 PM

What worked for me was, that I moved it to root on awake the first time. So I could leave it nested in my prefab.

         void Awake()
         {
             if (instance != null && instance != this)
             {
                 Destroy(this.gameObject);
                 return;
             }
             else
             {
                 // just move it to the root
                 this.transform.parent = null;
 
                 instance = this;
                 this.LoadAd();
             }
             DontDestroyOnLoad(this.gameObject);
         }
 
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 codecranker · Apr 20, 2013 at 03:15 AM

I didnt quite understood the logic behind defining a singleton MonoBehavior class. What do you gain by making it singleton?

why not just do DontDestroyOnLoad(gameObject) inside Awake() and then use GameObject.Find("singleton").GetComponent()?

Comment
Add comment · Show 3 · 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 whydoidoit · Apr 20, 2013 at 07:07 AM 1
Share

There are several reasons:

  1. It's much faster than using GameObject.Find

  2. It's more robust than GameObject.Find (if you change the name it still works)

  3. It provides intellisense in the code editor to find the object (you don't have to go and look it up in the editor)

  4. You can provide clever initialization to create the object if it doesn't exist

This is obviously even more useful if there are multiple components accessing the singleton.

avatar image sotirosn · Sep 27, 2013 at 08:20 PM 0
Share

Also Awake() is called each time a level is reloaded so if for example your player dies 3 times then you will have 3 high score keepers alive because each one called Don'tDestroyOnLoad().

avatar image Bonfire-Boy · Oct 28, 2017 at 11:59 PM 0
Share

$$anonymous$$aybe worth pointing out that what you're all talking about about here is just a particular kind of singleton. There's nothing to say a singleton monobehaviour has to use DontDestroyOnLoad at all.

avatar image
0

Answer by MaxLohMusic · Mar 28 at 08:44 AM

In line with the answer by sotirosn, in my case it was even simpler: The only thing I needed to do was move the object to not be nested under any other game object. Just make sure it's right under the scene. It then worked as expected. It's pretty incredible how this isn't even mentioned in the documentation. DontDestroyOnLoad does NOT work unless the object in question is not under any other object.

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

26 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

Related Questions

If Parent gameobject has DontDestroyOnLoad does it apply to its children? 1 Answer

Destroy on load/Properties Issue 2 Answers

Charge object on all scenes 0 Answers

DontDestroyOnLoad() and GameObject.FindGameObjectWithTag() [C#] 1 Answer

Dontdestroyonload input field 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