In this lesson, you will learn how to master the execution flow of your C# scripts by manually controlling code progression during a debugging session.
When your game behaves unexpectedly, simply pausing with a breakpoint isn't enough; you need to "drive" the code to see exactly where the logic diverges from your expectations. Once your IDE pauses execution at a breakpoint, you use three primary navigation tools found in your debugger’s toolbar:
The Navigation Trio
- Step Over (F10): This executes the current line and moves to the next one in the current method. Use this when you want to bypass a method call to see what happens immediately after it finishes.
- Step Into (F11): If the current line contains a method call, this moves the debugger inside that method. Use this when you suspect a bug is hiding within the logic of a specific sub-routine.
- Step Out (Shift+F11): This finishes the execution of the current method and returns you to the calling code. Use this when you’ve seen enough of a method and want to jump back to where it was invoked.
Practical Example
Imagine you have a TakeDamage method that isn't reducing player health correctly:
public void ApplyDamage(int amount) {
int finalDamage = CalculateDefense(amount); // Step Into here to check the math
currentHealth -= finalDamage; // Step Over here to see the new health value
CheckDeathStatus(); // Step Into here to see if the trigger works
}
By placing a breakpoint on CalculateDefense, you can use Step Into to walk through the math step-by-step. If you realize the math is correct, you can use Step Out to return to the ApplyDamage method immediately, then Step Over to watch the currentHealth variable update in your "Locals" or "Watch" window. By navigating line-by-line, you can pinpoint the exact moment a variable becomes corrupted or a logical condition fails to trigger, rather than guessing where the error originated.
Key Takeaway: Use Step Into to inspect sub-routine logic, Step Over to maintain flow control, and Step Out to quickly exit and return to the caller to maintain total visibility of your script's execution path.