Last active
September 14, 2020 00:52
-
-
Save tk009999/66a223a535fbbc0360a43cc643600765 to your computer and use it in GitHub Desktop.
#Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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