Setting up VSCode debugging for P3 development in Unity

You can get a long way debugging things simply by using a generous amount of logging statements and the Unity inspector. However, it is often faster and easier to just use a debugger. I was hesitant to bother setting it up, but now I wish I had done it earlier. Here is a quick guide on how to set it up, and a simple example of using it in P3SA.

Prerequisites

We need to install the Unity Debugger extension from the extension marketplace in vscode. Press CTRL+SHIFT+X and search for "Debugger for Unity". You will see 2 versions, a deprecated version from Unity, and a fork of that version by deitry. My understanding is that some compatibility changes were needed and the official extension did not support them. So install the deitry version.

Now we want to enable debugging for our project. Open the project in vscode. Click the Debug panel, and select "create a launch.json file" If you do this with a cs file open, it seems to look for c# extensions and doesn't find Unity as an option. But if you do it from another file, like the extension info page it works. Select "Unity Debugger".

We are now ready to debug something.

  • Load up Assets/Scripts/Modes/GameModes/MovingTargetMode.cs and add a breakpoint at line 30.

  • press F5 to start the debugger.
  • In Unity, run the program
  • Start a game with "s"
  • Click the rotating box with the mouse to hit target. Your program should stop on our breakpoint.

You can now do things like

  • Inspect variables
  • Add new variables to the watch list
  • Inspect the call stack
  • Step through line by line using the controls the red arrow is pointing to.

This is not intended to teach you everything you need to know about using the debugger, but hopefully it helps get you started.