Debugging. 🪲
Like it or not, it's a fundamental part of being a developer.
Over the past year, I've learned to like debugging, and even refactoring, my code. There's a certain sense of satisfaction that comes with cleaning up code and making it neat and tidy. Perhaps you've felt it too.
In this article, I'm going to walk you through the steps I take to debug my C# code as a Unity developer. Even if you don't work with Unity, hopefully this article will inspire you to reflect on your own debugging process and better appreciate all of the hard work you do to make your code shine. 🌟
When to debug?
Before I get started, I wanted to talk a little bit about when to debug in Unity. Generally, I don't think you need to fix a bug the second you find it, unless it's game-breaking. With newer versions of Unity, I've been having trouble testing my game in the editor without fixing error warnings first. This is a bit of a pain because it means I have to stop what I'm doing and immediately address each and every bug, even if it is minor and unrelated to the main game mechanics. But in the long run, I kind of appreciate being forced to do this since it means less trouble for my code down the line.
The Console is where debug messages appear in Unity. You can find the Console at the bottom of the Unity editor window.
Debugging in Unity: My Five Step Process
Now let's jump into the meat of this article and look at the five main steps I take each time I try to fix a bug in a Unity project.
Step One: Back up my project
I use Sourcetree to back up my work and my process looks like this:
Open up Sourcetree.
Stage any unstaged files in the project I'm backing up.
Write a blurb in the Commit text box to summarize the changes I've made. Sometimes I will just note that this is a backup commit of the project before I add a new feature or attempt some heavy debugging.
Hit Commit.
Hit Push to back my project up to a remote repository on GitHub.
PS. If you are a game developer who is looking for a solid introduction to using Unity with Sourcetree, I highly recommend GameDev.tv's Get Git Smart Course.
An image of the comment box for a commit in Sourcetree.
Step Two: Check for error codes or warnings in the Console
Next, I turn my attention to the Console and read any error messages or warnings that have popped up. Double-clicking these messages will open the script (if applicable) where the error occurs, and I can usually go from there.
Once you've worked in Unity for a while, you'll become familiar with certain types of errors.
Here are some examples you might see:
- Missing syntax like a semi-colon in a script (this is uncommon since you can't actually play your game with a syntax error like this; therefore, you'll usually catch this error in Visual Studio, well before it appears in the Console)
- Forgetting to add a scene to the Build Index in Build Settings
- Going outside the bounds of an array or list
- Missing a reference to a game object in either a script or the Inspector
- The dreaded Stack Overflow error (usually caused by a method referencing itself and thus creating an infinite loop!)
An image of a common error message in the Console. In this example, I have forgotten to add a scene to the project's Build Index.
Step Three: Check for problems in the Inspector
Often the Console will suggest that the problem is caused by an incorrect or missing setting in the Inspector. So the next thing I'll do is look at the Inspector and check whether anything is amiss there.
Here are some of the things to be on the lookout for in the Inspector:
- An empty serialized field
- A missing reference to a prefab or game object
- A missing component such as Rigidbody or Collider
- A missing script on a game object
- Using the wrong version of a Collider or Rigidbody (ie. 2D instead of 3D)
- Forgetting to hook up the OnClick() event for a button
- Using a wrong or misspelled tag/layer on a game object
An image of a common error message in the Console. In this example, I have forgotten to drag and drop a game object into a Serialized field in the Inspector.
Step Four: Check for problems in the scripts
Next, I'll open up my scripts and see if I can problem solve there.
Here are some common issues that you may run into:
- Trying to access a private method or variable from another class
- Forgetting to serialize a variable
- Using an incorrect or missing namespace
- Missing a reference to a game object
- Missing a reference to a component. This happens sometimes with GetComponent<>. You may be trying to access a component which does not currently exist on the game object. To solve this, add the component via the Inspector
Step Five: Start writing Debug.Log or print() statements
Once I've gone through the steps above and made sure that the problem isn't with something I've forgotten to do in the Inspector, then I'll start using Debug.Log or print() in my scripts. Usually by this time I have a pretty good idea of where the problem exists. I like to write my Debug.Log or print() statements with a string statement at the beginning so that I don't lose track of what I'm testing for.
So for example, if I was testing to see whether a variable was returning True or False, I'd write:
And the output would look like this in the Console:
I use this method to follow the line of logic through my scripts until I have located and fixed the problem. One last piece of advice: once you are done with a debug statement, make sure you either delete it or comment it out in your scripts. Otherwise your Console will become very cluttered with messages!
Now it's your turn: how do you tackle debugging in your projects? I'd love to hear any tips or tricks you have in the comments below.
Top comments (9)
Last month I decided to find a new way to have fun in my free time. After searching the Internet for a while, I found valorslot.in. What I particularly liked about this site is the ability to choose from a variety of different games with great graphics and sound effects. The ease of use and clear interface make the site accessible even for beginners in India. Thanks to this site, I have found a great place to have fun at any time.
No. I will not check anywhere for any additional information. You failed to motivate me. Go back and try again, if you like, but in principle, the internet is overflowing of information these days. So if you'll excuse me, I'll stay frigid to your calling. Also because I found tojiwin.net/ and already finished my search. They didn't ask me to check for information. They just showed what advantages I get from the game!
Great article! As a solo game developer myself, I appreciate the detailed breakdown of your debugging process in Unity. Your five-step approach is comprehensive and highlights the importance of methodically addressing issues to ensure a smooth development experience.
Backing up your project is crucial, and using Sourcetree with GitHub is a solid strategy. It's something I do as well, and it has saved me from many potential headaches. Checking the Console and Inspector for errors and warnings is a practice I follow too. It's amazing how many issues can be traced back to something as simple as a missing reference or a misconfigured component.
Writing Debug.Log statements is definitely a game-changer when it comes to pinpointing the root cause of a bug. Your advice to remove or comment out these statements after debugging is spot-on. A cluttered Console can make it much harder to identify new issues that may arise.
For anyone juggling game development with financial responsibilities, it's essential to manage your finances wisely. Loans with $750 cash app, debt, and credit cards can be both helpful and harmful. While they can provide the necessary funds to purchase development tools or software, they must be handled with caution to avoid accumulating unmanageable debt. Always have a repayment plan in place and consider the long-term impact of any financial decision.
I'm curious to hear how others manage their debugging process and balance their financial commitments. Any additional tips or insights would be greatly appreciated!
your work has been incredible, I am sure that developing a game is not easy at all, it takes a lot of time and dedication, I congratulate you, you are doing the work of an entire company by yourself, you are incredible
Hi. If you enjoy the thrill of gambling and are in search of a reputable online casino, look no further. I've been a dedicated player at this casino for an extended period, securing small wins on multiple occasions. Whether you're looking to play for real money or simply for entertainment, check here and register with this online casino for a rewarding experience
Thank you so much for all the different tips shared. I didn't know it was possible to do so much with JavaScript. I want to make an online gambling game similar to the ones you can find in click here for more information. I hope that throughout this process, the tips shared in your post will be quite helpful. I will come back to you if I have any concerns or if I encounter any difficulties.
I can only imagine how challenging and time-consuming it is to create the backrooms video game, so my sincere congratulations!
thanks a lot.
Amazing!