{"author_link":"\/users\/ausstein","author_name":"Ausstein","author_uid":"ausstein","comments":[],"epoch":1743808706,"event":"LD57","format":"md","ldjam_node_id":410896,"likes":9,"metadata":{"p_key":"209837","p_author":"Ausstein","p_authorkey":"1140199","p_urlkey":"449021","p_title":"Singleton Trick","p_cat":"LDJam ","p_event":"LD57","p_time":"1743808706","p_likes":"9","p_comments":"0","p_status":"WAYBACK","us_key":"1140199","us_name":"Ausstein","us_username":"ausstein","event_start":"1743811200","event_key":"111","event_name":"Ludum Dare 57"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD57","removed_author":false},"_superparent":406845,"_trust":9,"author":140199,"body":"Joining for my 14th LD in a row!\n\nAs a little bonus, I wanted to share a helpful Unity tip that I've been using in my last few projects \u2014 especially handy during a game jam when speed matters.\n\nIt\u2019s about the **Singleton pattern**, which is super useful for scripts where you only ever want one instance in your game (like your `SceneManager`, `AudioManage`r, etc.).\n\nI used to write the singleton logic manually in each of these scripts. But now, I use this handy base class instead \u2014 whenever I want singleton behavior, I just make my script derive from `Singleton<T>`!\n\nFor example:\n\n`public class SceneManager : Singleton<SceneManager>`\n\ninstead of \n\n`public class SceneManager : MonoBehaviour`\n\n\nI hope this is helpful to someone out there, here is the script:\n```\nusing UnityEngine;\n\npublic class Singleton<T> : MonoBehaviour where T : MonoBehaviour\n\n{\n\n    private static T _instance;\n\n    public static T Instance\n    {\n        get\n        {\n            if (_instance == null)\n            {\n                \/\/ Search for existing instance.\n                _instance = FindFirstObjectByType<T>();\n\n                \/\/ Create new instance if one doesn't already exist.\n                if (_instance == null)\n                {\n                    \/\/ Ensure that there's a GameObject to attach the singleton to.\n                    var singletonObject = new GameObject();\n                    _instance = singletonObject.AddComponent<T>();\n                    singletonObject.name = typeof(T).ToString() + \" (Singleton)\";\n\n                    \/\/ Make sure the singleton persists across scenes.\n                    DontDestroyOnLoad(singletonObject);\n                }\n            }\n            return _instance;\n        }\n    }\n\n    protected virtual void Awake()\n    {\n        if (_instance == null)\n        {\n            _instance = this as T;\n            DontDestroyOnLoad(this.gameObject);\n        }\n        else\n        {\n            Destroy(gameObject);\n        }\n    }\n}\n```\nGood luck to everyone jamming!","comments":1,"comments-timestamp":"2025-04-04T23:22:19Z","created":"2025-04-04T23:02:35Z","files":[],"files-timestamp":0,"id":410896,"love":9,"love-timestamp":"2025-04-05T00:34:30Z","meta":[],"modified":"2025-04-05T00:34:30Z","name":"Singleton Trick","node-timestamp":"2025-04-04T23:18:26Z","parent":410587,"parents":[1,5,9,406845,410587],"path":"\/events\/ludum-dare\/57\/dwarven-depth-runner\/singleton-trick","published":"2025-04-04T23:18:26Z","scope":"public","slug":"singleton-trick","subsubtype":"","subtype":"","type":"post","version":1286021},"node_metadata":{"n_key":"410896","n_urlkey":"449021","n_parent":"410587","n_path":"\/events\/ludum-dare\/57\/dwarven-depth-runner\/singleton-trick","n_slug":"singleton-trick","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"140199","n_created":"1743807755","n_modified":"1743813270","n_version":"1286021","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/57\/dwarven-depth-runner\/singleton-trick","text":"Joining for my 14th LD in a row!\n\nAs a little bonus, I wanted to share a helpful Unity tip that I've been using in my last few projects \u2014 especially handy during a game jam when speed matters.\n\nIt\u2019s about the **Singleton pattern**, which is super useful for scripts where you only ever want one instance in your game (like your `SceneManager`, `AudioManage`r, etc.).\n\nI used to write the singleton logic manually in each of these scripts. But now, I use this handy base class instead \u2014 whenever I want singleton behavior, I just make my script derive from `Singleton<T>`!\n\nFor example:\n\n`public class SceneManager : Singleton<SceneManager>`\n\ninstead of \n\n`public class SceneManager : MonoBehaviour`\n\n\nI hope this is helpful to someone out there, here is the script:\n```\nusing UnityEngine;\n\npublic class Singleton<T> : MonoBehaviour where T : MonoBehaviour\n\n{\n\n    private static T _instance;\n\n    public static T Instance\n    {\n        get\n        {\n            if (_instance == null)\n            {\n                \/\/ Search for existing instance.\n                _instance = FindFirstObjectByType<T>();\n\n                \/\/ Create new instance if one doesn't already exist.\n                if (_instance == null)\n                {\n                    \/\/ Ensure that there's a GameObject to attach the singleton to.\n                    var singletonObject = new GameObject();\n                    _instance = singletonObject.AddComponent<T>();\n                    singletonObject.name = typeof(T).ToString() + \" (Singleton)\";\n\n                    \/\/ Make sure the singleton persists across scenes.\n                    DontDestroyOnLoad(singletonObject);\n                }\n            }\n            return _instance;\n        }\n    }\n\n    protected virtual void Awake()\n    {\n        if (_instance == null)\n        {\n            _instance = this as T;\n            DontDestroyOnLoad(this.gameObject);\n        }\n        else\n        {\n            Destroy(gameObject);\n        }\n    }\n}\n```\nGood luck to everyone jamming!","title":"Singleton Trick","wayback_source":[]}