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 barbe63 · Nov 23, 2014 at 06:10 PM · dontdestroyonloadsingletonglobal variables

singleton, global vars and dontdestroyonload

Hello,

I have troubles carrying on my global vars trough scenes. I change the language setting in my menu ingame, then i launch a scene via a new game button and the setting is gone.

Here is my GlobalVar class:

 using UnityEngine;
 using System.Collections;
 
 public enum GameDifficulty
 {
     Easy,
     Medium,
     Hard
 }
 ;
 
 public enum Language
 {
     english,
     french
 }
 ;
 
 public class GlobalVars : Singleton<GlobalVars> {
     protected GlobalVars () {} // guarantee this will be always a singleton only - can't use the constructor!
     
     public GameDifficulty difficulty = GameDifficulty.Medium;
     public Language language = Language.english;
 
     void awake()
     {
         DontDestroyOnLoad(gameObject);
     }
 }


I also have a script in my project which contain a singleton class (used for an audio manager). Maybe it have something to do with this. Both are codes i didn't wrote myself and I'm so lost! Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
     protected static T instance;
     
     //Returns the instance of this singleton
     public static T Instance {
         get {
             if (instance == null) {
                 instance = (T)FindObjectOfType(typeof(T));
                 if (instance == null) {
                     GameObject container = new GameObject();
                     container.name = typeof(T)+"Container";
                     instance = (T)container.AddComponent(typeof(T));
                 }
             }
             return instance;
         }
     }
 }

Can you please tell me where i'm wrong?

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
0
Best Answer

Answer by fafase · Nov 23, 2014 at 06:25 PM

One issue I see here is that you have a constructor in your GlobalVars class which itself inherits from MonoBehaviour. That is not possible, you need to use Awake or Start.

Also a ctor does not guarantee one instance.

But what you are doing here is not related to persistence of data. There are already answers and even a 1h long video on that topic made by the guy from Unity.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

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 barbe63 · Nov 23, 2014 at 08:31 PM 0
Share

Thank you for the fast answer!

As i said those aren't my scripts unless the dont dontdestroyonload i put with no success... i just copied it from here: http://wiki.unity3d.com/index.php/Singleton

I'm pretty sure that guy on wiki did that ctor thing on some purpose no?

Anyway i'll watch that video and hope that will help me. Thanks again!

avatar image YoungDeveloper · Nov 23, 2014 at 09:08 PM 0
Share

You can also use static constructors to initialize or set data, which are quite helpful. http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx

avatar image barbe63 · Nov 23, 2014 at 09:12 PM 0
Share

But what you are doing here is not related to persistence of data

Not even with DontDestroyOnLoad ??? Now i'm confused.

avatar image fafase · Nov 23, 2014 at 09:18 PM 0
Share

The singleton thingy is not related, DonDestroyOnLoad is related. Check this for a list of possibilities:

http://answers.unity3d.com/questions/834693/how-to-call-script-of-other-scene-to-current.html

avatar image
0

Answer by barbe63 · Nov 23, 2014 at 10:46 PM

I just finished to watch the tutorial and checked your link also. i think i have pretty much what i need to build my persistent datas. Thanks folks!

Just so i know... the fail here was because it wasn't static? (cause i tried that but i didn't worked since it's instantiated). I don't really understand because in the wiki page this singleton method is described like a nice way to do it... but because i'm a foreigner and a beginner i may be misunderstood some parts.

edit : now i'm thinking i probably get confused between global variables and persistent datas... which are very different things.

reedit: hahaha i'm so stupid... actually my problem was very easy, i just forgot to put a caps on Awake() ;) Anyway i learned some nice stuffs too so i don't regret.

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

2 People are following this question.

avatar image avatar image

Related Questions

DontDestroyOnLoad duplicate object when using a singleton 5 Answers

Singleton DontDestroyOnLoad data persistance 0 Answers

Static variables and Design Fundamentals (singleton) 1 Answer

Unity Singelton - Dont destroy on load 1 Answer

Create a persistent GameObject using Singleton 3 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