Why does my singleton have two different instances?

I use the following template to make my singleton in Unity

public class BlobManager : MonoBehaviour  
{
    public static BlobManager instance {get; private set;}
    void Awake () 
    {
        if(instance != null && instance != this)
           Destroy(gameObject);

        instance = this;
        DontDestroyOnLoad(gameObject);
    }

    public void SomeFunction()
    {
       if (this != instance)
           Debug.Log("They're Different!")
    }
}

They always turn out to be different, as shown in SomeFunction(). When I set the value locally for the BlobManager class, and the other is to use the static "instance" of var like this:

foo = "bar"; 
BlobManager.instance.foo = "foo";

the foo value observed in the debugger at the breakpoint in the class will always be "bar". But when other classes try to access the same variable, it will be "foo".

I'm not sure how to look at memory addresses in Monodevelop, but I'm sure that thisthey this.instancewill have different memory addresses. Is there something wrong with my singleton pattern? I tried other templates with the same result.

+4
5

BlobManager , Instance.

MonoBehaviour , script , . Singleton , no MonoBehaviour, script . script , script, MonoBehaviour singleton

( ):

public class BlobManager
{
    static BlobManager _inst;
    public static BlobManager Instance {
    get
    {
        if (_inst == null)
            _inst = new BlobManager();
        return _inst;
    }
    }

    private BlobManager() 
    {

    }
}
+1

, monobehaviours

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    public static T Instance
    {
        get
        {
            if(mInstance == null && !isQuitting)
            {
                mInstance = FindObjectOfType(typeof(T)) as T;
                if(mInstance == null)
                {
                    GameObject go = new GameObject(typeof(T).Name);
                    mInstance = go.AddComponent<T>();
                    Logger.LogWarning("Creating " + go.name + " since one does not exist in the scene currently");
                }
            }
            return mInstance;
        }
    }

    public static bool HasInstance
    {
        get
        {
            //if we dont have one we try to find one
            if(mInstance == null)
            {
                mInstance = FindObjectOfType(typeof(T)) as T;
            }
            return mInstance != null;
        }
    }

    static T mInstance;

    protected static bool isQuitting = false;
    protected virtual void OnApplicationQuit()
    {
        isQuitting = true;
    }

}

, singleton , , Awake , Singleton, , , Awake .

singleton , , , .

+1

Singleton Monobehaviour. , , , , :

GameObject.Find("ContainerName").GetComponent<BlobManager>();

, Singleton. , -. .

0

Unity BlobManager . , , , , . , .

Awake , instance BlobManager, , , . , Destroy return, . , . instance , .

Awake:

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

, , . (. ).

, , : Unity singleton manager classes

0

singleton ,

, , ,

0

All Articles