• Register

Mire Studios is made up of two people who have always enjoyed playing games. We're gamers that make games so you can expect a wide variety of different games from us.

RSS My Blogs

Sometimes, you need globally accessible data. Although I'm not going to go into the whole thing debate about global data. I have noticed, that in Unity, a Singleton is sometimes a good way to move temporary data across scenes.

If you go search the web, you will see many examples of how to create Singletons in Unity. However, most of them require you to copy some boilerplate code into every script and having to copy code from one script into another is never a good idea. They also don't create themselves or do it in a weird way.

Here's our solution, a Singleton template. This abstract class puts all of the boilerplate into one script and has the bonus of the Singleton creating itself when it's requested by another script. This means, that if you forget to place a GameObject with the script into a scene, it will be created for you.

Below you will find two, slightly different examples of the Singleton template. I will explain them after showing the code:

    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static T _instance;
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    var create = new GameObject(string.Concat(typeof(T),  
                    " Singleton")).AddComponent<T>();
                    _instance = create;
                }

                return _instance;
            }
        }

        protected virtual void Awake()
        {
           if (_instance == null)
              _instance = this as T;
           else
              Destroy(gameObject);

            DontDestroyOnLoad(gameObject);
        }
    }

This implementation will simply create the object if it's null as you see in the getter. This is good if you have an object where the values are not exposed to the editor. If you are using a Singleton that has values that must be set in the editor. The following will suit you better:

    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static T _instance;
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    var create = Instantiate(Resources.Load(Prefab)) as T;
                    _instance = create;
                }

                return _instance;
            }
        }
        
        protected abstract string Prefab { get; }        

        protected virtual void Awake()
        {
             if (_instance == null)
                _instance = this as T;
             else
                Destroy(gameObject);

            DontDestroyOnLoad(gameObject);
        }
    }

This implementation requires that you have the object saved as a prefab in the Resources Folder. This will create the object, in case you forgot it, and will keep the values as it's a prefab. The main difference here is that the singleton will need to implement the "Prefab" field. Where you must simply just return the string name of the prefab you want to instantiate, easy.

Now to go over what these scripts do in more detail. If the singleton is already in the scene, a new one won't be created. Second, if there are multiple singletons the others will be destroyed so that there will only be one. Finally, to use the script, simply inherit from it and do what you would normally do with any other Singleton. The "T" parameter is simply the class that you pass in. Here's a quick example:

public class TestSingelton : MonoSingleton<TestSingleton>
{
   private void Start()
   {
      Debug.Log("I am a Singleton");
   }
}

Notice that you pass in the same class type as is inheriting the script. Pretty simple, right? If you any questions about these two Singleton templates, let us know in the comments below.

Until next time.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account:

X

Latest posts from @mirestudios

It's been a while but we've been working on a new game. #gamedev #indiedev #steam #2d T.co

May 9 2023

Battle Knights is currently on sale on #SteamAutumnSale. Check it out: Store.steampowered.com #indiedev #pixelart #gamedev

Nov 29 2020

It can be difficult deciding which game idea to go with. We currently have 4 different prototypes we've been debating on.

Oct 27 2020

RT @flibitijibibo: FNA 20.09 is now available, and FNA3D is now officially released: Github.com More details in the Disco… T.co

Sep 2 2020

Battle Knights on #XboxOne is on sale at 40% off right now. Check it out. Microsoft.com #indiedev #pixelart #gamedev

Jun 22 2020

Another screenshot of #BeebsAdventure. Weighted platforms, bumpers, and countdown blocks to mix up the platforming.… T.co

May 18 2020

A picture of #BeebsAdventure. We've been adding some new mechanics to the game. Beeb can wall climb and wall jump w… T.co

May 17 2020

Hey everyone, just a quick update. We decided to redesign some of the gameplay elements in Beeb's Adventure which i… T.co

May 11 2020

You can get Battle Knights and Neon Slashers on sale now! #pixelart #gamedev #indiedev #steamgame Store.steampowered.com

Feb 26 2020

Another screenshot of #BeebsAdventure. Stay tuned for more information on the upcoming demo which will be on… T.co

Feb 23 2020