// This method is called when the script is destroy protected void OnDestroy() { // If this is the currently active singleton, delete the static instance too if (singletonInstance == this) { singletonInstance = null; } }
// This method is called when the script is loaded protected void Awake() { // Check if there is already a singleton instance if (singletonInstance == null) // No? This is the first one { singletonInstance = this; } else // a instance already exists, destroy this { Destroy(this); } }