Set up VS Code for Unity
Although more of a text editor than an IDE, VS Code works with many available extensions to function as a full-scale IDE. It’s a popular code editor for web developers since it integrates seamlessly with the Node.JS package manager (npm).
You can download and install Visual Studio Code from its website, where you’ll find versions for Windows, macOS, or Linux.
After installing VS Code, you’ll need to set it as the external script editor via Unity > Preferences > External Tools in the Unity Editor. Under External Script Editor, choose Visual Studio Code from the drop-down menu. If VS Code doesn’t appear in the list, click Browse and locate the VS Code executable on your system.
Install the C# extension
You’ll need the extension called C# for Visual Studio Code to work with C# in Unity. To install the C# extension, open VS Code and click on the Extensions icon on the sidebar, or press Ctrl+Shift+X (Windows)/Cmd+Shift+X (macOS). Search for C# in the Extensions Marketplace, and click Install for C# for Visual Studio Code (powered by OmniSharp) by Microsoft.
Install Unity Debugger extension
In the Extensions Marketplace, search for Unity Debugger and install the Debugger for Unity extension. The version for Unity Technologies is no longer supported, but there is a community version by a publisher called deitry that you can install.
Finally, you also need to install the Visual Studio Code Editor package from the Package Manager.
Launching VS Code
Once your Unity project is open in VS Code, click the Run icon in the sidebar or press Ctrl+Shift+D (Windows)/Cmd+Shift+D (macOS) to open the Run view.
Click the Create a launch.json file link in the Run and Debug view. In the drop-down menu, select Unity Debugger as the environment. This will generate a default launch.json file with a preconfigured Unity debugging profile that should look similar to the following code example. You are now ready to debug in Unity.
Debugging using VS Code
There is a Play button in the left toolbar of the VS Code editor that’s marked with a bug icon. By clicking this, you’ll open the Run and Debug view. At the top of that view is a Play button and a drop-down list of launch options from the launch.json file. Select Unity Editor (it should already be selected by default since it’s the first launch option).
The Run and Debug view includes five windows for examining the state of your program: Locals, Watch, Call Stack, Breakpoints, and Exception Breakpoints (this last option is usually not used as frequently as the other four). These windows help you to inspect your code and understand the current state of your application while you are debugging. They work in a similar way to debugging windows in Visual Studio.
Add breakpoints by clicking in the margin or pressing F9 when the breakpoint line is selected in the code you want to test. Breakpoints in the editor margin will be shown as solid red circles.
Breakpoints can be edited by right-clicking, and conditions can be added to control the program breaking. When program execution is paused in VS Code, a toolbar is added to the view (similar to Visual Studio). The toolbar provides the following buttons:
- Play/Pause (F6)
- Step Over (F10)
- Step Into (F11)
- Step Out (⇧F11)
- Restart: Ctrl⇧F5 (Windows) and ⇧⌘F5 (macOS)
- Stop Debugging (⇧F5)
Leveraging the debugging windows
The Local window displays the values of all local variables in the current scope where the execution is paused. This includes variables declared within the method or function you are currently debugging, allowing you to use it to inspect the values of variables at the current point in your code execution.
The Watch window allows you to add specific variables or expressions that you want to monitor during the debugging process by manually adding any variable or expression to the Watch. You can see values being updated as you step through the code, a useful feature when you want to keep track of specific variables or expressions throughout the debugging session, even if they are not in the current scope.
The Call Stack window is useful for understanding the flow of your code and identifying how a particular piece of code was reached. It shows the sequence of method or function calls that led to the current point of execution, with each entry in the call stack representing a method or function call and the most recent call at the top. You can click on any entry in the call stack to navigate to the corresponding code in your project and view the local variables at that point in the execution.
The Breakpoint window shows a list of all breakpoints you have set in your code. Breakpoints are the markers that you place in your code to pause execution when a particular line is reached. You can use this window to enable or disable breakpoints, remove them, or navigate to the corresponding line of code in your project. This is useful for managing your breakpoints (if you have many) and for quickly jumping to specific locations in your code where you want to pause execution.
More resources for advanced Unity creators
Are you planning to use another code editor? You can read our article on Microsoft Visual Studio 2022.
Help boost team productivity with our e-book Create a C# style guide: Write cleaner code that scales. Get advice from industry experts on how to create a code style guide to help your team develop a clean, readable, and scalable codebase.
You’ll find many more productivity tips in 70+ tips to increase productivity with Unity. Improve your day-to-day aggregate workflow with Unity 2020 LTS, including tips even experienced developers might have missed out on.
Find all of Unity’s advanced e-books and articles in the Unity best practices hub.