Tier 2 Series 101

101E: File I/O Safety

Pilot Record
Student Profile
"Storing flight logs in the cockpit cup holder is a bad idea. If the plane changes, the logs are lost. In Unity, trying to save files to "C:/MyGame" will crash on mobile or consoles. A skilled Engineer audits for Path Safety."

The Concept: Persistent Data Paths

When saving data (like JSON or XML), you cannot just save it to "C:/GameSaves" because that folder doesn't exist on an iPhone or a PlayStation.

* **Application.persistentDataPath:** A magic Unity variable that always points to a safe, writable folder on ANY device (Windows, Mac, iOS, Android).
Red Flag Detected

The AI Trap: "The Hardcoded Path"

You ask the AI: "Save the player's score to a file."

// AI-Generated Code: Guaranteed to Crash on Mobile
void SaveScore(string data) {
    // Audit Fail: This path only exists on your PC.
    File.WriteAllText("C:/Saves/score.txt", data);
}

This is "Platform Ignorance." This code will work fine on your laptop, but the moment you build for Android, the game will crash because it doesn't have permission to write to the root C: drive.

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
// Corrected: Cross-Platform Safe
void SaveScore(string data) {
    string path = Path.Combine(Application.persistentDataPath, "score.txt");
    File.WriteAllText(path, data);
}
Your Pilot Command
> Use the Interface IPointerClickHandler for cleaner UI interaction logic.
Next Mission
Prefab Logic