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 kevinspawner · Apr 07, 2015 at 05:39 PM · dontdestroyonloadsingleton

Unity Singelton - Dont destroy on load

Am not quiet sure what exactly is the issue in my code below. Am not able to save the game object with its component between the scene. I have created the singleton and dont destroy on load.

Problem: Once I get back to the scene all the buttons and other components are not working at all. Is there any issue with my code?

     private static startScreen_Manager _startScreen_ManagerInstance;
     public  static startScreen_Manager   startScreen_ManagerInstance        
     {
         get 
         {
             if(_startScreen_ManagerInstance == null)
             {
                 _startScreen_ManagerInstance = GameObject.FindObjectOfType<startScreen_Manager>();
                 DontDestroyOnLoad(_startScreen_ManagerInstance.gameObject); // Unity dont delete this object on level load.
             }
             return _startScreen_ManagerInstance;
         }
     }
 
     void Awake()
     {
 
         if(_startScreen_ManagerInstance == null)
         {
             _startScreen_ManagerInstance = this;
             DontDestroyOnLoad(this);
         }
 
         else
         {
             if(this != _startScreen_ManagerInstance )
             {
                 Destroy(this.gameObject);
             }
         }
             
     }
 
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 kevinspawner · Apr 09, 2015 at 03:01 AM 0
Share

Anyone? Thanks.

avatar image smallbit · Apr 09, 2015 at 03:22 AM 0
Share

First your singleton getter and Awake function are kinda duplicating itself. Secondly what are the buttons? are they childs of the gameObject your singleton was in? Its hard to tell without your full code (i.e. how you declare objects that are "not working". $$anonymous$$eanwhile I can tell you the problem I have once had, so I had a singleton in once scene that was having reference to buttons (which were childs of the same prefab). Singleton declaration is persistent across the scenes, even if you don't use "don't destroy on load", singleton will keep all the references to the object and the variables as it was initialized. So I was initializing my singleton, than loading next stage, than co$$anonymous$$g back to first scene, and when I wanted to use the button I was getting an error "gameobject you want to use has been destroyed". The thing was, when reloading scene the buttons were "recreated" but the singleton kept references to the old initial gameobjects. So to solve it I just cleaned the Instance in OnDestroy method, and on start I create new Instance which will than load references to current buttons. Other solution for that would be to make the button GameObjects to prefabs.

avatar image kevinspawner · Apr 10, 2015 at 03:01 AM 0
Share

Thanks. I will try your idea. Am still confused with this singleton.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mapleman · Apr 09, 2015 at 03:35 PM

For me your code looks overly complicated. Since in Unity scripts are attached to gameobjects, I assume that you have somekind of empty 'controller' gameobject which you would like to preserve between scene loads. So, in your scripts awake-method call:

DontDestroyOnLoad(gameObject); //gameObject is the reference to your controller GameObject which has the script (in this case startScreenManager) attached to

That's that. Because when you call inside Awake DontDestroyOnLoad(this), the this keyword actually points to your script. Well, it might be preserved (but probably not), but anyway the 'hosting' gameobject is destroyed and new one is created after scene load. And when new gameobject is created it's Awake method get's called. And since it's new object all it's fields and members are set to default values. Meaning that all references are null.

Comment
Add comment · Show 6 · 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 kevinspawner · Apr 10, 2015 at 02:59 AM 0
Share

Thanks. Let me break down the hierarchy, I should have explained it better in my question, anyways.

I have a [empty]game object called game$$anonymous$$anager. Added a audio listener component and the script to it. Basically the script contains the function for the button [controls audio on and off]

Yes! You are right, am bit confused about this DontDestroyOnLoad(gameObject);

I used DontDestroyOnLoad(); But when the new scene is loaded everything get reset, So I thought probably singleton is the the one that I need to rely on.

Can you explain me what is the best way to keep the game object and all its component in the same state when the scene loads? Do I need to remove the awake lines? So, all the associated game components are active?

avatar image Mapleman · Apr 10, 2015 at 03:46 AM 0
Share

You shouldn't need to do anything to keep the states and references intact between scene loads as long as you are correctly calling the DontDestroyOnLoad(gameObject). Awake and Start are called only once when object is instantiated first time. And if the object doesn't get destroyed, then these methods won't get called again as long as the game session is running.

avatar image kevinspawner · Apr 10, 2015 at 09:31 AM 0
Share

I did tried that. The problem is, I have attached audio component with audio enable/disable button. So when the scene loads if the user mute the sound and if he comes back again to the same scene, I want the button to remain muted and sound disabled. But the problem is again it restarts.

avatar image Mapleman · Apr 10, 2015 at 10:44 AM 0
Share

That's strange. I use exactly same kind of setup in my game, and it works like a charm. Are you absolutely sure that all objects taking part in your audio logic are contained within same gameobject?

avatar image kevinspawner · Apr 13, 2015 at 02:34 AM 0
Share

Thanks. Yeah am quiet confused with this as well. I have a game object [Game $$anonymous$$anager] and audio component is only referenced through the script attached to this game object. No other game object is sharing the component. Also, when the user click the mute, the sprite is supposed to remain muted. But even though I add 'DontDestroy' Still everything resets on level change.

Show more comments

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

20 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

Related Questions

Preventing my game object from multiplying due to DontDestroyOnLoad() 2 Answers

Singleton reproducing every other scene load 0 Answers

How to keep assets in memory without duplication? 1 Answer

Unity C# Singleton? 6 Answers

How do I ensure no more than one instance of each of many GameObjects? (Singleton doesn't seem to scale well this way.) 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