Skip to content


Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Let me show you TWO WAYS to DEBUG UNITY GAME or CODE written in VISUAL STUDIO in Unity 2021! Don´t publish buggy games!

By the way! If you are interested in becoming a professional software developer, make sure to check out our courses!

And find the video to this article here!


Man those bugs are thriving in this hot weather!. But worry not! because I will teach you how to smash them all in Unity : ). But first, we need to debug them :D.

This tutorial will discuss two ways to debug any unity game or code written in Visual Studio in general.

I have a game called “Cookie Quest”. It’s a game about…. cookies.


The game is simple. You click on the cookie, you get a cookie!. and the background color will change randomly!

But sadly, sweet things attract bugs!. So let’s smash them all before they eat all of our cookies.

So let’s run the game and click on the cookie in the middle.

And nothing, no errors no nothing.

That’s not very promising so let’s apply what we have learned step by step for each bug we faced!

Recognize that a bug exists.

Which we already did since we saw that nothing happened and we also know how to reproduce it by …. clicking on the button.

Isolate the source of the bug.

Since we are clicking on a button, our button might not be working correctly.

So let’s double-check that.

Let’s select the button under our canvas.

Yep, there is no method assigned to our button.

Identify the cause of the bug.

If we look at the button we can clearly see that the button is linked to a script called UIManager, but the button itself is not linked to any specific method.

If we click on the UI Manager in the OnClick part of the button the linked gameobject with the script in question will blink in yellow.

Now let’s select the UI Manager gameobject. and open the script attached to it.

Inside this script, we can see that this script contains only one method.

If we look at the OnCookieButtonClick method we can see that it tries to change the background color of the camera to a random color and then calls the AddCookie method from the ScoreManagerScript.

Which is exactly what we want!

Determine a fix for the bug.

So a straightforward fix for this bug would be simply to connect the button to this method.

Apply the fix and test it.

So let’s assign this method to our button and smash this bug once and for all!.

Now let’s run the game and click on the button again.

[Enter drums effect] Aaaaaaand!

It appears that there is one error.

Repeat the process to DEBUG UNITY GAMES!

So let’s read it!.

It says null reference exception, which means we are trying to access a variable or a property that has the value null. In other words, it’s unassigned.

Thanks to Visual Studio Tools for Unity that we installed with Unity, we can use visual studio to debug our game!.

Since this issue is more code-related, let’s debug it using visual studio!.

One of the perks of debugging using the visual studio is something called Break Points.

Breakpoints will cause our game to pause and we will be able to view the values of the objects in our code at that point.

Breakpoints will help us to isolate and identify the cause of the bug. So let’s add two breakpoints, one point for each line of our code in the OnCookieClick method.

So we will repeat the steps of debugging again : ).

Then click on attach to unity, to start debugging.

Now breakpoints will only trigger if the lines assigned to them were executed.

So Hit Play in Unity and click on the button.

Visual Studio will start to blink, so let’s check it out.

We can see the yellow line where our code stopped executing.

If we hover over main in Camera.main.BackgrounsColor.

It says that it’s null!.

Well, that explains everything.

Now we know that the issue is with our main camera. So let’s stop Debugging and check the camera in our scene : ).

Can you guys see the error? I will give you a moment.

Yep!. Our main camera is not tagged as MainCamera.

So an appropriate fix for this issue is to give our camera the correct tag since Camera.main will find the main camera by tag.

Phew!. Finally. So let’s clear the console, hit play, and click on the button Again : ).

They… Multiply !!!!!.

The background color changes randomly now.

But this error wasn’t here before when we clicked on the button!.

In Test Oriented Development this is known as “Defect Masking”.

This means basically that bugs will cover up for each other.

We had more code with bugs in technical terms, but we never saw them because that code was never executed since when a line of code throws an exception, this will cause the execution to break/stop at that line.

Basically, this line of code was never reached, at least not until this point.

So let’s go inspect the AddCookie Method!.

From what we can see the AddCookie method will do the following.

  1. Get the current score as a string from the UI text.
  2. Convert it to an int.
  3. Increase it by 1.
  4. Finally, assign it back to the UI text.

So let’s add a breakpoint to the lines that could possibly be the cause of the bug to isolate it.

Now attach to Unity and hit play in the Editor.

And click on the button.

The first breakpoint will trigger.

If we hover over scoreText.Text we will see that it display the text correctly.

So let’s hit continue to continue the execution.

The next breakpoint will trigger.

Ahaaa!. The issue is that we can’t convert “Text” to a number !.

So to fix this we need to set the text to “0” as a string.

Now we are already at step 4.

Determine a fix for the bug.

So we have two possible fixes that I can think of.

  1. Set the text to 0 in the Editor like this.

But this might cause issues in the future since we forgot to set it to “0” once already, then we might have the displeasure of seeing this bug again, Yes! these bugs are like zoombies! they come back to life.

So let’s end it once and for all by implementing a fail-proof fix!.

  1. The second possible fix is setting scoreText.text to “0” at the start method.

Let’s do that.

Now let’s run the game : ).

Once we run the text is set to 0. So that’s good.


Now click the button.

Finally! 😢

Conclusion to DEBUG UNITY GAMES tutorial.

As you have seen, we were able to fix all of the bugs relatively quickly. But we spent most of our time searching for them. Now there is a final tool that I still didn’t tell you about. But this one is on you!.

It’s called Patience. Which you will need a lot during the debugging process : ).

And remember smashing a bug a day keeps the crash away : ), if you smashed a bug today get yourself a cookie, you deserve it ;).

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide