Using Debugger in Visual Studio with Unity

One of the most important things in programming and only second to using Google is knowledge about debugging your code! Surprisingly not many people know how to do it, so let me show you! This will be an example of how to use Visual Studio for that purpose.

What debugging is?

Debugging is a process of finding errors or bugs in the code. There is a lot of different approaches to doing it. Some involve printing a lot of console logs, and others require you to track which code is executed.

Depending on which way you prefer to work, you might need an IDE that has a debugger. Visual Studio has great integration with Unity[link?], so as long as you are using it, you don’t need to worry about it. If you are using something else, you should double check that.

How to use debugger?

To demonstrate how to debug code using Visual Studio Debugger with Unity, we need a project. I’m going to use the project from the post about object pooling, but you can do it on yours.

After finding our project, we can go and open Visual Studio with our project. When the project is loaded, we have to attach Debugger to it. In my case we have to do to Run > Attach to Process. This should open the window in which we have to choose the process or the program to which we want to attach to. For programs made in Unity, this will be the Unity Editor process.

Attaching to the Process option
List of processes

With Debugger attached to our project, now it’s time to add breakpoints in our code!

Let’s put our first break point where we are getting a new instance from the Factory.

Creating breakpoint

Now we can run the game and wait until the breakpoint is triggered! This shouldn’t take long. ?

When the breakpoint is triggered, the game will be stopped, and we can see what is going on in our code. We can also navigate and execute the code line by line now using buttons on!

Code execution buttons
Call Stack window

I also need to mention a handy feature, which can really help you. I’m speaking about Watch list, where you can preview values of variables from your code!

Taking a look at values in variable
Variable’s Watch List

Modifing code

After you found some bugs in your code, there is one thing that you have to remember.

You shouldn’t edit your code while the debugger is attached to the project. This sometimes freezes Unity and Visual Studio.

So to save yourself from having to kill them, don’t forget to detach it! ?

Great! You’ve learned how you use the debugger for debugging your code! You can be proud of yourself!

I hope you liked this post! You can share it with your friend that definitely need to learn how to do it. ?

Did you know about the debugger before? Leave me a comment bellow!

Other than that, I will see you soon! ?

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x