Tier 2
Series 106
106B: The Key Ring
"`PlayerPrefs` is a dictionary. It relies on "Keys" (Strings). If you save as "Score" but try to load as "score", you get zero. An Engineer uses `const string` keys to prevent typos."
The Concept: Key Management
Preventing "Magic String" errors.
* **Magic String:** Typing "Gold" manually in 5 different scripts.
* **Risk:** One typo breaks the save system silently.
* **Fix:** A central file holding all keys.
* **Magic String:** Typing "Gold" manually in 5 different scripts.
* **Risk:** One typo breaks the save system silently.
* **Fix:** A central file holding all keys.
Red Flag Detected
The AI Trap: "The Typo Hazard"
You ask the AI: "Save and Load the gold."
// AI-Generated Code: Fragile Strings
// Script A:
PlayerPrefs.SetInt("PlayerGold", gold);
// Script B:
int g = PlayerPrefs.GetInt("playerGold"); // Lowercase "p"
This is "Case Sensitivity Failure." The Load function returns 0 because "playerGold" does not exist.
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 Engineer's Correction
Corrective Protocol
// Corrected: Type Safety
public static class Keys { public const string GOLD = "PlayerGold"; }
PlayerPrefs.SetInt(Keys.GOLD, 10);
Your Pilot Command
> Tell the AI to use a ScriptableObject for the Player Stats to make data persistent between levels.