Skip to content

Instantly share code, notes, and snippets.

@tk009999
Last active September 14, 2020 00:52
Show Gist options
  • Save tk009999/66a223a535fbbc0360a43cc643600765 to your computer and use it in GitHub Desktop.
Save tk009999/66a223a535fbbc0360a43cc643600765 to your computer and use it in GitHub Desktop.
#Unity3D
using UnityEngine;
public class DontDestroyOnLoadController<T> : MonoBehaviour where T : MonoBehaviour
{
public static T Instance;
private object _lock = new object();
protected virtual void Awake()
{
lock (_lock)
{
Debug.Log(Instance);
if (Instance != null)
{
Destroy(gameObject);
}
else
{
Instance = gameObject.GetComponent<T>();
DontDestroyOnLoad(gameObject);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment