Skip to content

Instantly share code, notes, and snippets.

@ShutovKS
Created May 6, 2024 17:02
Show Gist options
  • Save ShutovKS/4aaf894c9305140683c33ef7db7a3014 to your computer and use it in GitHub Desktop.
Save ShutovKS/4aaf894c9305140683c33ef7db7a3014 to your computer and use it in GitHub Desktop.
Example Singleton "MonoBehaviour"
using UnityEngine;
public class MonoBehaviourSingletonExample : MonoBehaviour
{
public static MonoBehaviourSingletonExample Instance => _instance ?? Initialize();
private static MonoBehaviourSingletonExample _instance;
private static MonoBehaviourSingletonExample Initialize()
{
var instance = new GameObject();
_instance = instance.AddComponent<MonoBehaviourSingletonExample>();
DontDestroyOnLoad(_instance);
return _instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment