Tier 1
Series 6
6E: Singleton Sanitation
"There can be only one Captain on a ship. If a second Captain walks onto the bridge, the first one must either leave or throw the new one overboard. If you have two AudioManagers playing music at the same time, you have "Instance Collision"."
The Concept: The Singleton Pattern
A Singleton is a script that ensures only one copy of itself exists. It provides a global "Instance" variable so anyone can find it.
Global Access: GameManager.Instance.Score
Persistence: Usually paired with DontDestroyOnLoad().
Sanitation: It must destroy duplicates of itself upon waking up.
Global Access: GameManager.Instance.Score
Persistence: Usually paired with DontDestroyOnLoad().
Sanitation: It must destroy duplicates of itself upon waking up.
Red Flag Detected
The AI Trap: "The Duplicate Captain"
You ask the AI: "Create a GameManager that persists across scenes."
// AI-Generated Code: Dangerous Duplication
void Awake() {
DontDestroyOnLoad(this);
// Audit Fail: If you return to the Main Menu and
// load the game again, a second GameManager is created.
}
This is "Instance Collision." You will end up with 2, then 3, then 4 GameManagers running simultaneously, all fighting for control.
Elite Telemetry
Research shows "Elite" teams achieve 15% faster lead times by keeping AI on a "very tight leash."
- Small Batches Solving one problem at a time prevents logic drift.
- Modular Design Localizing the "blast radius" of AI changes.
- Tight Loops Rapid iteration with constant code review.
The Mechanic's Correction
Corrective Protocol
public static GameManager Instance;
void Awake() {
if (Instance == null) {
Instance = this;
DontDestroyOnLoad(gameObject);
} else {
Destroy(gameObject); // Throw the imposter overboard
}
}
Your Pilot Command
> A skilled Mechanic directs the AI to use the Singleton Pattern. You command: "Implement the Singleton pattern to ensure only one instance exists."