I use the following pattern 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 end up being different as shown in SomeFunction()
. When i set a value locally for the class BlobManager and another for using the static var "instance" like so:
foo = "bar";
BlobManager.instance.foo = "foo";
the value of foo a seen in the debugger on a breakpoint within 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 im sure this
and this.instance
would have different memory addresses.
Is there something wrong with my singleton pattern? I've tried may other patterns as well with the same result.