Skip to content

Instantly share code, notes, and snippets.

@Novack
Forked from j4rv/LazyngletonBehaviour.cs
Created October 25, 2024 21:39
Show Gist options
  • Save Novack/de47e8893ff31ef39751bb9d3c9bd375 to your computer and use it in GitHub Desktop.
Save Novack/de47e8893ff31ef39751bb9d3c9bd375 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class LazyngletonBehaviour<T> : MonoBehaviour where T : LazyngletonBehaviour<T> {
private static T _instance;
public static T Instance {
get{
if(_instance == null){
GameObject newInstance = new GameObject();
_instance = newInstance.AddComponent<T>();
newInstance.name = _instance.GetType().Name;
_instance.OnFirstCall();
}
return _instance;
}
}
protected virtual void OnFirstCall () {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment