Singleton : Static Property or Instance Properties ?

I’ve been using Singletons in Unity for some time now and I’m looking for an advise regarding their implementation.

For now I’m using this :

public class GeneralSettings : MonoBehaviour
{	
	private GeneralSettings _instance ;
	public GeneralSettings Instance
	{
		get
		{
			if(_instance == null)
			{
				_instance = (GeneralSettings) GameObject.FindObjectOfType(typeof(GeneralSettings)) ;
				
				if(_instance == null)
				{
					GameObject go = new GameObject("Singleton::GeneralSettings") ;
					_instance = go.AddComponent<GeneralSettings>() ;
					Language = (LANGUAGE) LANGUAGE.Parse (typeof (LANGUAGE), PlayerPrefs.GetString ("Language")) ;
				}
			}
			return _instance ;
		}
		set
		{
			_instance = value ;
		}
	}
	
	private LANGUAGE _language = LANGUAGE.French ;
	public LANGUAGE Language
	{
		get { return _language ; }
		set
		{
			if (value != _language)
			{
				_language = value ;
				PlayerPrefs.SetString ("Language", _language.ToString()) ;
			}
		}
	}
}

Now, this means that in every other script, I access the settings with GeneralSettings.Instance

I was wondering what would be the effects/benefits of using static properties instead, like this :

public class GeneralSettings : MonoBehaviour
{	
	private static GeneralSettings _instance ;
	public static GeneralSettings Instance
	{
		get
		{
			if(_instance == null)
			{
				_instance = (GeneralSettings) GameObject.FindObjectOfType(typeof(GeneralSettings)) ;
				
				if(_instance == null)
				{
					GameObject go = new GameObject("Singleton::GeneralSettings") ;
					_instance = go.AddComponent<GeneralSettings>() ;
					Language = (LANGUAGE) LANGUAGE.Parse (typeof (LANGUAGE), PlayerPrefs.GetString ("Language")) ;
				}
			}
			return _instance ;
		}
		set
		{
			_instance = value ;
		}
	}
	
	private static LANGUAGE _language = LANGUAGE.French ;
	public static LANGUAGE Language
	{
		get { return _language ; }
		set
		{
			if (value != _language)
			{
				_language = value ;
				PlayerPrefs.SetString ("Language", _language.ToString()) ;
			}
		}
	}
}

Thanks !

Well it’s partly down to style and partly down to how you store such properties and what you put in them.

An Instance singleton will have all of its variables cleared when the instance is destroyed. But static fields would not be destroyed necessarily - perhaps meaning that you hold on to resources for longer than you need.

For instance:

   public static List< Mesh> interestingMeshes;

Would hold those meshes between scenes, perhaps when you didn’t want to actually do that.

Having an Instance property would remove this issue. Now presuming you actually accessed your members via properties that used the Instance internally then there would be no difference except less typing for you! However, the contract of a static property would be “kind of” broken, given that the static property would actually be accessing an instance member.

For me, I do both, depending on the context. I err on the side of Instance or Current members for my singletons to avoid the problems listed above, however, there are times when it’s beneficial to go down the static member route - I do that occasionally and with caution.

Where I do always use statics is for things that list out all of the instances of items.

  public static List< MyClass> allMyClasses = new List< MyClass>();

  void OnEnable() { allMyClasses.Add(this); }
  void OnDisable() { allMyClasses.Remove(this); }