/// <summary> /// Resets the instance reference to null when the player is destroyed. /// Specifically, during scene loading, but also for any other reason. /// </summary> private void OnDestroy() { if (this == _instance) { _instance = null; } }
/// <summary> /// Sets the instance reference to itself whenever a new instance is created. /// If another one exists, it will log an error and destroy itself. /// Note: We might want to change it so that it destroys the OTHER instance, if it turns out to be easier in practice. /// </summary> private void Awake() { if (_instance != null && _instance != this) { Debug.LogError("[PlayerInstance]: There should never be more than one PlayerInstance. Destroying new Instance."); Destroy(this.gameObject); return; } _instance = this; }