AI Command Lab // Flight Simulator
2C: Iteration Loops
Initial AI Output (Fragile)
// AI-Generated Code (Will Crash)
foreach (var drone in activeDrones) {
if (drone.battery <= 0) {
activeDrones.Remove(drone); // ERROR: Collection modified!
}
}
Initial AI Prompt
"You ask the AI: "Write a loop to check a list of active drones and remove any that have run out of battery.""
Pilot Comm Link
Mission Objective: Direct the AI to use a LayerMask to optimize the Raycast search.
Optimized Protocol
// Corrected Code (Stable & Safe)
for (int i = activeDrones.Count - 1; i >= 0; i--) {
if (activeDrones[i].battery <= 0) {
activeDrones.RemoveAt(i); // Safe removal
}
}