Singletons in Unity

February 13, 2024

In this article, you will be provided with all you need to know about Singletons in Unity.

What are Singletons?

In Unity, a singleton is a class that exists only once in a scene. They are design patterns that are used to create a class that exists only once throughout an entire project. That is, a singleton class has only one instance. This instance exists across all scenes and scripts. This enhances centralized functionality.

A singleton is a globally accessible class that holds static references to itself meaning that other scripts can access the references’ variables and methods directly using just the class name. Due to its globally accessible property, it is recommended to protect the reference with a private set property. It is just a class with a private constructor to protect its reference that returns a single instance.

Why are singletons used?

Here are the common use cases of Singletons in Unity:

  • Since singletons are globally accessible, they come in handy in creating game managers. That is, singletons can efficiently manage the global aspects of a game.
  • Also, due to the accessibility of singletons, it enhances the connectivity of different parts of your game. This is because scripts can use it without needing to set up a reference first.

How to create a Singleton in Unity

Creating a singleton in Unity is very straightforward, especially for beginners. To create a singleton in Unity, you just need to create a class with a private constructor. As said earlier, the private constructor protects the reference, thereby preventing external instantiations. Then, declare a static instance. Then check if the instance exists or not.

Here’s a basic example script:

using UnityEngine;

public class SingletonExample: MonoBehaviour {
    private static SingletonExample _instance;
    public static SingletonExample Instance

    {
      get
      {
        if (_instance == null)

        {
          _instance = FindObjectOfType<SingletonExample>();

          if (_instance == null)
          {
            GameObject singletonObject = new GameObject(typeof (SingletonExample).Name);

            _instance = singletonObject.AddComponent<SingletonExample>();
          }
        }

        return _instance
      }
    }

    private void Awake()
    {
      if (_instance != null && _instance != this) //to ensure only one instance exists
      {
        Destroy(singletonObject)
      }
    }
}

In the script above, a singleton class called SingletonExample is created from a MonoBehaviour class. Then the ”Instance” is a static reference of the singleton class built with a private constructor. This is a single instance that other scripts can access. The Awake method is used to ensure that there’s only one instance of the singleton class.

How to access a Singleton from other scripts in Unity

Now, that we know how to create a singleton in Unity, how can the singleton be accessed from other scripts?

You just need to call the Instance method of the singleton class.

private void Start()
{
  SingletonExample singleton = SingletonExample.Instance;
}

You can now access any public methods or variables defined in the SingletonExample class.

Bottom Line

Singletons tend to make your code hard to manage and maintain as it gets bigger and bulkier. This leads to debugging issues. Therefore, it is recommended to only use singletons when truly necessary to enhance code design and maintainability.

Want to build better games?

© 2024