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?
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.
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.
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!
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!
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! ?