- Home /
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
}
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. :)
@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.
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
}
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.
@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.
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.
Your answer
Follow this Question
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