Essential Systems / Singletons

Singletons

Enjoying Essential Systems?Leave a review

Learn to use the included singletons.

Singleton

A singleton for any MonoBehaviour. The instance must exist in the scene.

public class Test : Singleton<Test>
{
    public void TestMethod()
    {
        Debug.Log("Test");
    }
}

public class AnotherTestClass : MonoBehaviour
{
    private void Start()
    {
        Test.Instance.TestMethod();
    }
}

SingleSingleton

Like Singleton, but the instance persists across scenes via DontDestroyOnLoad.

public class Test : SingleSingleton<Test>
{
    public void TestMethod()
    {
        Debug.Log("Test");
    }
}

public class AnotherTestClass : MonoBehaviour
{
    private void Start()
    {
        Test.Instance.TestMethod();
    }
}

SingletonScriptableObject

A singleton for a ScriptableObject. The asset must be placed in a Resources folder.

[CreateAssetMenu(fileName = "Test", menuName = "Essentials/Test")]
public class Test : SingletonScriptableObject<Test>
{
    public void TestMethod()
    {
        Debug.Log("Test");
    }
}

public class AnotherTestClass : MonoBehaviour
{
    private void Start()
    {
        Test.Instance.TestMethod();
    }
}
Last updated