Skip to content

Files

Latest commit

 

History

History
42 lines (38 loc) · 1.19 KB

README.md

File metadata and controls

42 lines (38 loc) · 1.19 KB

Unity-Singletons

A collection of inheritable Singleton scripts for Unity

How to use
Simply inherit any of your scripts from either Singleton<T> or SingletonDontDestroy<T> and that script will automatically inherit it's own instance and be made available as a singleton

Inheriting from Singleton<T> will simply make the object a singleton whereas inheriting from SingletonDontDestroy<T> will ensure the object persists between scenes by adding it to the DontDestroyOnLoad list.

Examples
Inheriting from the given classes

public class ScoreManager : Singleton<ScoreManager>
{
    private int score;
    public void AddScore(int i)
    {
        score += i;
    }
 }
public class ScoreManager : SingletonDontDestroy<ScoreManager>
{
    private int score;
    public void AddScore(int i)
    {
        score += i;
    }
 }

Calling the classes instance from elsewhere in your project

public class Enemy
{
    private int scoreForKill = 100;
    public void OnKill()
    {
        ScoreManager.instance.AddScore(scoreForKill);
    }
}