I am working on a iOS game and I need a Singleton gameObject that takes care of certain this in my game. I understand that when I want an object not to be destroyed I call the DontDestroyOnLoad (); function. I do this and it works like I want it to it stays around and I have all the functionality I want. The only problem is that when I return to my MainMenu (where this Singleton object is created) I end up with 2 singleton objects. And every time I return I get more and more. I understand this behaviour when I think about it but unfortunately I don’t know how to solve this problem. Is there someone that is knows how to solve this? If additional information such as code is needed I will off course provide this.
Thanks in advance.
Well yes it is a normal gameObject and because the MainMenu is my first scene I created this singleton there so I am able to access it anywhere even in my MainMenu. Is there a way I can prevent it from instantiating it again when this object already exists?
It as a init function for attempting to init the singleton. At the beginning of the game I call this to initialize ( funct TryInitializeManager )… note that if for whatever reason I try calling it again, it doesn’t do anything.
Now I may need reference to a gameobject to hook into certain game events. Such as in the case of the StateManager which is a member of the Game singleton. In that I create a gameobject, flag it as not destroyed, and hook into it for all said events. The StateManager is not a singleton though, it’s a member of a Singleton. And I have a Dispose method on it to destroy the gameobject incase I ever have to destroy it.
A singleton is a class that only allows a single instance of itself to ever exist.
If the class does nothing to disallow more than one… how is it a singleton? Then it’d just be an object that you only created once.
You can get the same effect by only creating it once. But you need all other code to ensure it does so. A Singleton protects itself, enforcing that requirement that it’s the only one.
Here is a simple example of a class enforcing it:
public sealed class Game
{
#region Singleton Interface
private static Game _inst;
public static Game Instance
{
get
{
if(_inst == null) _inst = new Game();
return _inst;
}
}
private Game()
{
}
#endregion
}
note that it’s sealed (no class can extend this class), and the constructor is private.
also note that this one lacks an init function like I have in the one I linked. And instead this one creates my object in the Instance property getter if it’s null. The one I linked has an init function becasuse I have parameters I must pass in. Both are reasonable implementations of this pattern.
I think it stems from a confusion in the Unity documentation. The unity documentation says that all classes will inherit from MonoBehavior when writing C#. But this is wrong… all components/behaviors should, but not ALL classes. But the ambiguity of the wording by Unity confuses the unexperienced.
Check to see if it already exsists… if not instantiate it. Otherwise do nothing. The following script is in unityScript.
I use the premade “GameManager” tag for my game builds and include my PlayerPrefs code there. Attach this to anything other than the singleton inside your menu screen. Usually I use the camera.
var prefab : GameObject; //Store prefab gameManager here.
var gM = GameObject.FindGameObjectWithTag("GameManager"); //grab it if exsists
if(!gM){
Instantiate(prefab, Vector3.zero, Quaternion.identity); //Create a new one!
}
Yes oke I understand that, but how can I make it that it allows itself to only be created once? (I am not quiet sure if this sentence is correct english but I hope you understand what I mean)
Couldn’t agree more. The issue is not so much in deriving from MonoBeaviour (although it does cause issues which you pointed out in your previous post) but in attaching that “singleton” to a GameObject. What you’ve done then is essentially wrap a singleton class in a non-singleton class. Since there’s nothing stopping you from cloning the parent GameObject you can no longer guarantee that a single instance exists.
Yes I have read both. but some posts were coming all at the same time thats why I missed a couple posts. sorry if it looked like I was not reading what you were posting. it was just all going a bit fast :).
But I understand the code you posted here. I think I am also getting a better understanding of this concept. The code in the link you posted I found a bit more difficult but I do get what is happening.
Thank you very much I think that I must be able to manage now.