Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 theredace · Apr 28, 2018 at 04:15 PM · singletonstatic variable

How does a singleton maintain a persistent value?

I use singletons regularly and they work just fine, but I was thinking about exactly how they work and some things don't make sense to me. For the sake of argument, I'm using this basic structure of a singleton:

 public static SingletonExample Instance {get; private set;}
 
     void Awake()
     {
         if (Instance == null)
         {
             Instance = this;
             DontDestroyOnLoad(gameObject);
         }

         else
             Destroy(gameObject);
     }

Okay, so based on this I have some questions:

  1. Does DontDestroyOnLoad() actually makes a duplicate copy of the object into the next scene? It seems to me that it does, considering that if I return to the original scene again I end up with 2 copies of the object, hence the main reason for using a singleton (to destroy the duplicate).

  2. When I return to the original scene I temporarily have 2 copies of my object until my duplicate singleton destroys itself. During that time before the duplicate gets destroyed, what is my static Instance referencing if I have 2 of them in the scene and it can only reference one thing?

  3. When I return to my original scene, which copy of my singleton runs its Awake() method first? This is actually the original reason for me trying to understand this, because the singleton specifically destroys THIS gameObject, not the OTHER gameObject, which worried me that it would not maintain any non-static changes that have occurred in the class since it was duplicated into another scene. But of course this does actually work... If I bring my singleton into scene 2, update a value in it, then bring it back to scene 1 the values remain updated in scene 1. How is that possible if I'm destroying the copy of the singleton that I brought with me from scene 2 back to scene 1, and keeping the version of the singleton from scene 1 which did not get updated? The only possible reason I can think of is that actually the original singleton that was left in scene 1 runs its Awake() method first and destroys itself, leaving behind the version that was brought back from scene 2 which does have the updated value that was changed within scene 2. But that would also require that my static Instance variable was only referencing the original when I returned to scene 1, making the reference null once that original gets destroyed... otherwise the other singleton (which came back from scene 2) would also destroy itself because it would run that check 2nd, see that Instance was not null, and destroy itself.

This all leads me to believe that I am wrong somewhere in my assumptions here. What am I misunderstanding?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by theredace · Apr 28, 2018 at 04:23 PM

And as usual, I figure out the answer to my question right after I post it...

It appears that the Awake() method doesn't get called on an object when it's brought between scenes using DontDestroyOnLoad(), which means that when I return to scene 1, ONLY the original singleton object is running its Awake() method and destroying itself, keeping the one that I bring between scenes.

I'll leave the question up in case somebody else is wondering the same thing.

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

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

Singleton and multiple scene data 1 Answer

Data from a simple singleton / static class return null 1 Answer

Static List< GameObject> in Singleton 1 Answer

singleton becomes null? 0 Answers

Objects of global singleton aren't accessible 1 Answer


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