AI Command Lab // Flight Simulator
102A: The Component Audit
Initial AI Output (Fragile)
// AI-Generated Code: Fragile
public class PlayerJump : MonoBehaviour {
void Jump() {
// Audit Fail: What if there is no AudioSource?
// NullReferenceException: Object reference not set to an instance of an object
GetComponent<AudioSource>().Play();
}
}
Initial AI Prompt
"You ask the AI: "Write a script that plays a sound when the player jumps.""
Pilot Comm Link
Mission Objective: Direct the AI to use UI Toolkit (UI Builder) for modern, responsive layouts.
Optimized Protocol
// Corrected: robust and self-healing
[RequireComponent(typeof(AudioSource))] // Automatically adds the component
public class PlayerJump : MonoBehaviour {
private AudioSource audioSource;
void Awake() {
// Safe to assume it exists because of the attribute above
audioSource = GetComponent<AudioSource>();
}
void Jump() {
audioSource.Play();
}
}