301A
The Global Compass
The Concept: Singletons & DontDestroyOnLoad
public static ScoreManager Instance;
void Awake() {
if (Instance == null) {
Instance = this;
DontDestroyOnLoad(gameObject);
} else {
Destroy(gameObject); // Clear the deck of duplicates!
}
}
301D
Cross-Scene Comms
The Concept: Global Events
// The Signal (Broadcaster)
public class QuestManager : MonoBehaviour {
public static event Action OnQuestCompleted;
public void CompleteQuest() {
OnQuestCompleted?.Invoke(); // Broadcast to whoever is listening
}
}
// The Receiver (Subscriber)
public class AudioManager : MonoBehaviour {
void OnEnable() => QuestManager.OnQuestCompleted += PlaySound;
void OnDisable() => QuestManager.OnQuestCompleted -= PlaySound;
}
301F
The Garbage Collector
The Concept: Resource Management
public class MissionUI : MonoBehaviour {
void OnEnable() => MissionManager.OnUpdate += Refresh;
// Corrected: Severing the link when the UI is disabled
void OnDisable() => MissionManager.OnUpdate -= Refresh;
void Refresh() { /* Update UI logic */ }
}