Unity Singleton GameObject

I’ve seen a few singleton patterns flying around for Unity GameObjects, and thought I’d contribute my own. It will destroy duplicates of the MonoBehaviour, so you can create instances in code and have a GameObject in every scene with your singleton script attached and it won’t matter.

using UnityEngine;
 
namespace UnityHelper
{
    public abstract class SingletonMonoBehaviour<T> : MonoBehaviour
        where T : SingletonMonoBehaviour<T>
    {
        private static GameObject msSingleton = null;
 
		//
		public static string SingletonName
		{
			get
			{
				return typeof(T).ToString() + "?Singleton";
			}
		}
 
        //
        public static T Instance
        {
            get
            {
				T retVal = null;
 
				// If the singleton pointer is null
				if (msSingleton == null)
				{
					// See if the GameObject already exists
					msSingleton = GameObject.Find(SingletonName);
 
					// If it doesn't then create it
					if (msSingleton == null)
					{
						msSingleton = new GameObject();
					}
				}
 
				// With the GameObject, get our singleton monobehaviour
				retVal = msSingleton.GetComponent<T>();
 
				// If it doesn't exist yet, create it
				if (retVal == null)
				{
					retVal = msSingleton.AddComponent<T>();
				}
 
				return retVal;
            }
        }
 
        //
        private void Awake()
        {
			// If the singleton pointer isn't set yet, set it from this object
			if(msSingleton == null)
			{
				msSingleton = this.gameObject;
			}
 
			// If this monobehaviour is assigned to another GameObject, remove that GameObject
			if(msSingleton != this.gameObject)
			{	
				DestroyObject(this.gameObject);
			}
 
			// Otherwise, initialise this monobehaviour, this is our singleton instance
			else
			{
				msSingleton.name = SingletonName;
				msSingleton.hideFlags = HideFlags.DontUnloadUnusedAsset;
				DontDestroyOnLoad(msSingleton);
 
				OnSingletonCreated();
 
				if (Debug.isDebugBuild)
				{
					Debug.Log("Singleton Created : " + SingletonName);
				}
			}
        }
 
        //
        private void OnDestroy()
        {
			// If this is our singleton instance, then we do want to terminate our singleton
			if(msSingleton == this.gameObject)
			{
				msSingleton = null;
 
				OnSingletonDestroyed();
 
				if (Debug.isDebugBuild)
				{
					Debug.Log("Singleton Destroyed : " + SingletonName);
				}
			}
 
			// Otherwise it is just a duplicate instance
			else
			{
				if (Debug.isDebugBuild)
				{
					Debug.Log("Duplicate Singleton Destroyed " + SingletonName);
				}
			}
        }
 
		// These should be used instead of OnAwake() and OnDestroy() to prevent
		// duplicate instances setting up
		protected abstract void OnSingletonCreated();
		protected abstract void OnSingletonDestroyed();
    }
}

Since OnAwake() and OnDestroy() will get called as well for the duplicates, I’ve added two new methods OnSingletonCreated() and OnSingletonDestroyed() that should be used instead.

Now to implement, just make the monobehaviour you wish to be a singleton inherit off the generic.

namespace MySingletonProject
{
    public sealed class DispatcherMonoBehaviour : UnityHelper.SingletonMonoBehaviour<DispatcherMonoBehaviour>
    {
		//
		protected override void OnSingletonCreated()
		{
			// What you would normally do onAwake
		}
 
		//
		protected override void OnSingletonDestroyed()
		{
			// What you would normally do onDestroy
		}
 
        //
        void Update()
        {
		// do something
        }
    }
}

This code will allow you to destroy the GameObject through normal means as well if you want to be rid of the singleton until it is needed again.

Wallet

IMG_20150607_215151
I don’t usually keep my business card there

I’ve been using the same wallet since I worked in Subway in Bolton and a special girl gave me my first one. I’ve had to fold and glue the elastic several times, repair the coin pouch, and just stop using slots.

I’ve been looking for a new one for a while, but none seem to have the charm or the right configuration of pouches. I think its quite a common thing from every man I’ve spoken to, finding that right wallet for you.

IMG_20150607_215204However recently I found the perfect replacement on Amazon. I was a little hesitant with the price and some of the negative reviews, but nothing can beat the benchmark slow cooker star-curve of godliness. Buying a wallet is like buying a bed or sofa, you need to try before you buy.

slowcooker
The benchmark of buying anything off Amazon

However I got lucky. It is a good weight and size, and the coin pouch is hidden behind more slots. The only flaw is the zipper occasionally falls behind the cards, but it is a minor issue I can learn to live with. And it doesn’t have any sentimental value of being a gift.

Nexus 5 and giffgaff

Screenshot_2015-06-03-00-01-54I just upgraded my phone from the Nexus 4 to the Nexus 5. Unfortunately I lost a few numbers that I only realised after I formatted the old device. I’m sure I’ll be contacted by anyone missing within time.

I’m glad I sorted out Guild Wars 2 before I formatted, it is quite a hassle to remove the authenticator and requires the original 2 step, before I could add a new one.

The Nexus 5 supports 4G, however my current giffgaff goodybag is currently 3G. I’ve changed it so that the next goodybag will be a 4G package, however that is a month away. There is a way to cancel a goodybag, you need either less than £5 of credit, or less than 5MB remaining. I opted for the second one, and began to download a 500MB file, which will then be followed by another 500MB file until I have exhausted my goodybag.

Framed Pictures

KantoA while back I bought a few pictures from society6, and I finally got around to buying some frames for them to hang them up. The one above is Kanto from Pokémon, I contacted the creator @Melee_Ninja on twitter after seeing it.

IMG_20150418_124341

I love Journey, and it makes a great frame. I went through several choices before I finally decided on this one.

IMG_20150426_162826These are my most recent additions, Orihime from Bleach, and Mikasa from Attack on Titan.

IMG_20150413_211203Finally, Lavos and Crono from Chrono Trigger, in an epic stand off. I was tempted to mount this poster, however with it being such a dark image already, I couldn’t find a mount colour that worked well with it.

The frame is a P70 on the website I linked.