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
0
Question by Cygon4 · Aug 16, 2013 at 04:03 PM · dontdestroyonloadlifetime

DontDestroyOnLoad() fails in Player

I have this helper class that I use to store components that should stay alive even when changing scenes:

 public static class Session {
 
   /// <summary>Name of the game object that will carry the session state</summary>
   public const string SessionGameObjectName = "Session";
 
   /// <summary>Retrieves or create the game object holding the session state</summary>
   /// <returns>The game object responsible for holding the session state</returns>
   public static GameObject GetOrCreate() {
     if(sessionGameObject == null) {
       sessionGameObject = GameObject.Find(SessionGameObjectName);
 
       if(sessionGameObject == null) {
         Debug.Log("===== Session game object not found, creating a new one");
         sessionGameObject = new GameObject(SessionGameObjectName);
       }
 
       GameObject.DontDestroyOnLoad(sessionGameObject);
     }
 
     return sessionGameObject;
   }

   /// <summary>Game object that is holding the session state</summary>
   private static GameObject sessionGameObject;

 }

In the Unity editor, it works flawlessly.

However, after compiling, in the player, when a new scene is loaded, GetOrCreate() does not find the supposedly protected Session game object and creates a new one each time.

Am I using DontDestroyOnLoad() wrong or what's going on here?

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

2 Replies

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

Answer by Cygon4 · Aug 16, 2013 at 04:14 PM

It seems that I can solve my issue this way:

 public class DontDestroyOnLoadCaller : MonoBehaviour {
 
   void Awake() {
     DontDestroyOnLoad(this.gameObject);
   }
   void Start() {
     DontDestroyOnLoad(this.gameObject);
   }
 
 }

Adding that component to the Session gameo bject right before the return sessionGameObject; line makes everything work as expected.

So it seems DontDestroyOnLoad() may only be called from Awake() or Start() of a MonoBehaviour in the same GameObject for some reason.

Comment
Add comment · Show 2 · 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 Joyrider · Aug 16, 2013 at 04:19 PM 0
Share

No I'm calling $$anonymous$$e in a Coroutine called from a function that is called from Start. And my destroy on load applies it to an array of objects. So it is not limite to the object the script is on. And I doubt it is limited to the Start or Awake functions.

On the other hand, in your example, if the sessionGameObject is never null, the DontDestroyOnLoad is never applied to it. So maybe it is already assigned when you call your GetorCreate function?

avatar image Cygon4 · Aug 17, 2013 at 10:57 AM 0
Share

In my example sessionGameObject is only ever assigned through the GetOrCreate() method I posted, so if it's non-null, DontDestroyOnLoad() will have been called on it.

I'll have to do some more testing to find out what exactly causes DontDestroyOnLoad() to be ignored. $$anonymous$$aybe calling new GameObject() and then DontDestroyOnLoad() directly after is the culprit.

It's also interesting that it does work perfectly in the editor and only fails when I create a standalone build of my game.

avatar image
1

Answer by Xtro · Aug 16, 2013 at 04:11 PM

Hey, where do you declare the sessionGameObject variable? I can't see it here. I think it must be a "static" variable in this class. Right ?

Comment
Add comment · Show 1 · 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 Cygon4 · Aug 16, 2013 at 04:16 PM 0
Share

Yes, I forgot to copy & paste that line. Thanks for pointing it out :)

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

17 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

Related Questions

DontDestroyOnLoad() does not seem to be working. 3 Answers

FindGameObjectsWithTag not finding a tagged object 4 Answers

DontDestroyOnLoad duplication. 0 Answers

C# Unity 3d Don't Destroy Class On Load 1 Answer

Setting public GameObject to a different Prefab through code 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