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 /
avatar image
5
Question by zoon · Nov 16, 2010 at 10:11 AM · gameobjectdontdestroyonload

DontDestroyOnLoad - is it intended behavior?

Today I found out that DontDestroyOnLoad function clones host gameObject at level reload. It seems to me, that before Unity 3.1 it acted differently (unfortunately I dont have other versions at hand). Is it OK or is it a bug? alt text

Comment
Add comment · Show 1
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 Jesse Anders · Nov 16, 2010 at 10:32 AM 1
Share

It seems that should be the expected behavior, yes? Every time you reload the scene a new 'dummy' object will be created; since each of these objects will persist from level to level, you end up with multiple dummy objects. (A more typical use of DontDestroyOnLoad() would be, I think, to apply it to an object in e.g. scene A, and then progress through scenes B, C, etc. In this context, the single object created in scene A would persist through any subsequently loaded levels.)

3 Replies

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

Answer by duck · Nov 16, 2010 at 10:33 AM

I think this behaviour was present in previous versions. It is not that DontDestroyOnLoad is cloning the gameObject, it is that the non-destroyable-object is persisting (as it should), but re-loading the level is also loading a new copy of the object (because it exists in the scene file).

If you want to reload the scene which contains the "Don't Destroy" object, but you don't want two copies of that object, you need to add some code which makes the 2nd new copy of the object destroy itself, leaving the first to persist. For example, a static boolean variable could be used to do this:

 private var _sofar : float;
 private static created = false;
 
 function Awake() {
     if (!created) {
         // this is the first instance - make it persist
         DontDestroyOnLoad(this.gameObject);
         created = true;
     } else {
         // this must be a duplicate from a scene reload - DESTROY!
         Destroy(this.gameObject);
     } 
 }
 
 function Update() {
     _sofar += Time.deltaTime;
     if (_sofar > 3) {
         print("reloading level ...");
         Application.LoadLevel("test");
     }
 }
     

         
Comment
Add comment · Show 4 · 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 ciabaros · Aug 04, 2012 at 10:43 PM 0
Share

An alternate approach is to not pre-create this persistent "state" object in your hierarchy at all, rather have every scene check if the object exists, and create it dynamically (i.e. using some "LoadState" script), only if it doesn't already exist. This way, you always have just one copy of this object, and it doesn't matter what scene you start in.

avatar image littall ciabaros · Dec 05, 2016 at 07:42 AM 0
Share

duck's way is preferred for better code maintenance, since one only has to care about persistence in one place.

avatar image siddharth3322 · Feb 03, 2014 at 08:46 AM 0
Share

This works absolutely fine.

avatar image Naphier · Jun 23, 2015 at 08:07 AM 0
Share

There's also Persistent Singleton patterns that are really good for this.

avatar image
3

Answer by ciabaros · Aug 04, 2012 at 10:48 PM

An alternate approach to Ben's answer is to not create this persistent "state" object in your hierarchy at all, and have every scene check if the object exists, and create it dynamically (i.e. using some "LoadState" script), only if it doesn't already exist. This way, you always have just one copy of this object, and it doesn't matter what scene you start in.

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 Mike 3 · Nov 16, 2010 at 10:32 AM

That's normal, including versions before 3.1

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Destroy Object when scene loads 1 Answer

DontDestroyOnLoad does not work after reload the scene? 1 Answer

overlap between scenes!!! 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

DontDestory gameobject doesn't work on button click after a new scene loads 2 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