Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 andrew_2992 · Dec 03, 2016 at 03:41 AM · c#dontdestroyonloadsingletonlogic

Singleton and DontDestroyOnLoad question

Hi, I'm using a singleton design to keep the player profile persistent between scenes. I'm having a problem when I return to the main menu from a level.

Lets call my main menu scene A and a level in the game scene B.

in scene A I have about 30 or 40 different variables for different gameobjects, text components, image components etc. When I load scene B, I have some scripts which will access the static singleton and read data from it, the singleton persists through to scene B as expected however when I return to scene A all of the references to the variables in that scene are gone. I was expecting that scene A would load again and my menu would still have all of the references.

Here's the code I'm using for the singleton...

 public class GameController : MonoBehaviour {
 
     public static GameController Controller;
 
 void Awake () {
         
         if (Controller == null) {
 
             DontDestroyOnLoad (gameObject);
             Controller = this;
         } else if (Controller != this) {
             Destroy (gameObject);
         }
 
     }
 
 }

I've been reading about DontDestroyOnLoad but I'm having a bit of trouble wrapping my head around how to get over this one. A possible solution I've been thinking might be to not destroy the object when I go from scene A -> B ... but then have some sort of flag I can set up to destroy it when moving back to scene A (B -> A). Does anybody have any thoughts or ideas on how I might implement such a solution? I welcome any other ideas.

Another thing to note is that I don't actually need any of the data in the singleton object by the time I make it back to scene A (the main menu). By that time I've saved the gamer profile and if I could completely reload the scene , destroy the singleton and create a new instance when scene A loads, then I think that'd work perfectly. The logic inside the awake function is what I could really use help with.

Thanks everybody, -Andrew

UPDATE :

I haven't found a clean elegant solution to this, but I figure the solution would be to use another method in the first place so for now I've found a way around... What I initially wanted was to put some logic in the awake function to handle destroying the singleton when I returned to the main menu. So what I did was add a removeInstance() function to the gamecontroller class and then from my Racemanager script(the script which is linked to the button click to go back to the main menu, I simply call the Gamecontroller.controller.RemoveInstance() function and it runs Destroy(gameobject). So when I return to the main menu and there is no active gamecontroller object it just creates a new one.. Seems to work....

Comment
Add comment · Show 2
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 hexagonius · Dec 03, 2016 at 07:54 AM 0
Share

I don't get the linkage between the 30 and 40 variables you mentioned and who has them? Are they part of your singleton?

avatar image andrew_2992 · Dec 03, 2016 at 04:01 PM 0
Share

Yes theyre part of the singleton (gamecontroller class). Ins$$anonymous$$d of finding them through script i have dragged them into the field in the inspector. I left them out of the code but assume that there would be objects such as Public Image menubackground; Public text credittext; etc.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by JC_SummitTech · Dec 04, 2016 at 08:30 AM

When you call DontDestroyOnLoad, the object and all it's children will persist through scenes. It does not however ensure that the objects that it refers to are kept; those will be destroyed.

When you have such an object, you need to make sure that it does not refer to stuff that is not contained within the object itself. If that is not an option, instead of keeping references to gameobjects, just keep the values themselves. For example, instead of keeping a reference to your volume slider, just have the object write the value of the slider, and then read it back.

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

246 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Question About Singleton Pattern 2 Answers

DontDestroyOnLoad Duplicating Objects When Scene Reloaded 1 Answer

UNITY2D: How do I destroy previous object with Singleton Pattern? 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