Debugging Scripts in Unity
By: James West
Tags: Game Development, Gamedev, Gamedevhq, Debugging, Unity3d

Enter the Orkin Man

Debugging Scripts in Unity

We have a bug to squash

If you have gone through any online course or watched YouTube videos about how to make games in Unity you have come across the advice to use Debug.Log() or Debug.LogError() in order to troubleshoot the games you are making. Now I am not here to tell you to stop using those, they can be helpful in isolation small issues when you are kind of aware where to look but you can find yourself wasting a lot of time putting these in and removing them from your scripts. They are expensive to use and clutter things up unnecessarily.

There is a better way!

If you have never written code before the idea of debugging might seem foreign to you. That’s OK. It really isn’t all that difficult to get started with, which is what we will do today.

Overview

Debugging code is the practice of walking through your code while it is running to see if the results you expect are occurring. This is especially handy when you are not sure exactly where an issue is originating from.

Most modern IDEs allow you to use debugging tools. Today’s article isn’t about choosing the best IDE. I will be using JetBrains Rider because it is my editor of choice and integrates easily with Unity. However VS Code can also debug Unity scripts if you have the proper plugin installed, Unity Debugger Extension for Visual Studio Code. Visual Studio also has the ability to perform these same functions.

You can read more on the details of setting up your favorite over on the Unity — Manual: Debugging C# code in Unity (unity3d.com)

Getting Started — Add a Break Point

I am assuming you already have your IDE of choice installed and working with Unity. As previously mentioned I am using JetBrains Rider 2021 so all steps from here forward are using JetBrains.

First, we need to open the script we want to debug and set some break points.

Breakpoint- this is where the code will stop executing automatically and require you to step it through line by line.

Setting a breakpoint

Setting breakpoints is simple and works the same in most code editors.

Here we have a method I want to debug. Now I could add Debug.Log() messages to the console at every location I want to review, but as I mentioned that is just too slow. So I will set a breakpoint so I can debug.

The breakpoint is denoted as the little red circle that is added to the window. I move the mouse around to find the spot I want then left click to add the break point.

OK, so this is where I will stop my code execution so I can debug.

Before we move on, note that we can add multiple breakpoints all over our code. This will become handy later.

Running the Scene in Debug Mode

Breakpoint in place? Check. Let’s debug this bad boy and make sure things are running as intended.

The debug button in the tool bar

To start we can press the Debug button located in the toolbar at the top right. This is the same as clicking the play button in Unity. The editor will attach to Unity and tell it to play and your game will play as normal, until it hits the break point. This happens to be when my player’s damage method is invoked, so lets see that in actions.

The Unity Engine asking what you want to do

If this is your first time using the script debugging then Unity will prompt you to select an option. Only for this session will ask you the next time you reopen Unity and try to debug, and all projects will always debug without showing this window.

Runs until stops

The game runs until the player takes damage. This happens when the ship crashes into the player model. Notice how the game freezes? This is what we want to happen. Now if we jump back to our editor of choice we should notice something has changed.

Code stopped at break point

Notice the arrow? The check marked breakpoint and the highlighted line? This means our code is currently stopped at this location. It can not continue until we tell it do so. We also have a new window on screen (again this is JetBrains Rider) called Debugger.