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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Keratos · Jul 25, 2014 at 09:45 PM · dontdestroyonloadsingleton

Objects of global singleton aren't accessible

I am trying to create some kind of global class storing player data which could be accessible in the whole project. I created simple singleton using this code:

 void Awake () {
     if(Instance == null) {
         Instance = this;
         DontDestroyOnLoad(gameObject);
     }
     else Destroy(this);
 }

Unfortunately only simple variables (like int, string) are acessible. Objects in other scenes are null. What can I do to make them alive just like their simple brothers?

@Edit: More code:

 using UnityEngine;
 using System.Collections;

 public class PlayerInfo : MonoBehaviour {
     public static PlayerInfo Instance;
 
     public string Name;
     //string static Password;
     public int Money;
     public byte Flat;
     public byte Gender;
     public long CreateDate;
 
     public Skills Skills;
 
     void Awake () {
         if(Instance == null) {
             Instance = this;
             DontDestroyOnLoad(gameObject);
         }
         else Destroy(this);
     }
 
 
 }
 
 [Serializable]
 public class Skills{
     public byte dexterity;
     public byte vocabulary; // talking skillz
 
     // add more
 
 }



Comment
Add comment · Show 7
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 jamesflowerdew · Jul 26, 2014 at 12:29 AM 0
Share

I think you need to share more data. From what I can tell, you have successfully created an object that will survive the loading of a scene. "Instance" needs to be static or you could end up with many instances of your singleton, all with seperate data. anything that you really want to keep can be static too ("public static $$anonymous$$yWeirdthing[] all$$anonymous$$yWeirdThings;"), but then you can't set it in the inspector, and normal non-static stuff should survive with your object anyway. This might be obvious but if you store a camera variable and then delete the camera (by loading a scene for example), your camera variable will be null. Are the values referring to something that has changed or been removed? hope this helps. :)

avatar image Keratos · Jul 26, 2014 at 02:15 AM 0
Share

@jamesflowerdew I added some extra code to provide you the whole object. As i wrote before $$anonymous$$oney, Flag, Gender, CreateDate and Name work fine but Skills even after being declarated in one scene in another is null. I'm looking a way to fix it.

avatar image jamesflowerdew · Jul 27, 2014 at 12:38 AM 0
Share

I got errors with your code straight out of the tin. Does this help?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerInfo : $$anonymous$$onoBehaviour {
       public static PlayerInfo Instance;
      
     public string Name;
     //string static Password;
     public int $$anonymous$$oney;
     public byte Flat;
     public byte Gender;
     public long CreateDate;
      
     public Skills Skills;
      
     void Awake () {
     if(Instance == null) {
     Instance = this;
     DontDestroyOnLoad(gameObject);
     }
     else Destroy(this);
     }
      
      
     }
      
     [System.Serializable]
     public class Skills{
     public byte dexterity;
     public byte vocabulary; // talking skillz
      
     // add more
      
     }
avatar image jamesflowerdew · Jul 27, 2014 at 12:51 AM 0
Share

also of note of you edit in one scene it will update for all scenes in run-time, but it will not update all scenes in the editor, so you'll only see that data set by running the scene that you edit. if you want one game object that you can change in any scene to update all scenes in the editor, prefabs are the way. makr this object a prefab, drag it into all scenes and then only edit the prefab. then you'll have total consistency in editor too.

avatar image Keratos · Jul 27, 2014 at 11:27 AM 0
Share

@jamesflowerdew i forgot to comment out one more line in my code however your code still doesn't make Skills survived in another scene. I don't really care about data shown in editor because the only thing I need is a global accessible data keeper.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerull22 · Jul 28, 2014 at 02:44 PM

Here is the basic model of a singleton that I like to use for data storage containers that are always supposed to exist across all scenes.

 using UnityEngine;
 
 /// <summary>
 /// Is a singleton construct.
 /// </summary>
 public class MyClass : MonoBehaviour
 {
 
     /// <summary>
     /// Instance of the MyClass class.
     /// </summary>
     /// <remarks>
     /// Please use this for all calls to this class.
     /// </remarks>
     public static MyClass Instance
     {
         get { return _instance ?? (_instance = new GameObject("MyClass Game Object").AddComponent<MyClass>()); }
     }
 
     private static MyClass _instance;
 
     private void Awake()
     {
         //If instance is already set and this script is not it, then destroy it.
         //We already have a MyClass in this instance.
         if (_instance != null && _instance != this)
             Destroy(gameObject);
         _instance = this;
         DontDestroyOnLoad(this);
     }
 }
 

So internally in the script you will always use "_instance". But from everywhere else you'll use "Instance". Provides some data protection level for you.

Also you don't need to attach this script to a game object in your scene. Since when Instance is referenced, if _instance is null, then a new game object will be created in your scene that will not be destroyed on load. It's actually a very nice script for a singleton in Unity.

I can't remember where I got this from, but I did grab this from someone else, so I can't take credit for it. I just made some minor alterations to it. I hope it helps.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to keep assets in memory without duplication? 1 Answer

Singleton DontDestroyOnLoad data persistance 0 Answers

Singletons in music script? 2 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