Debugging in Unity | How to fix errors in your project | Beginners Must Learn This

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video we look at how to debug and fix issues in unity so this is one of the most important skill to have because if you are a beginner you will run into lots of issues and if you don't know how to diagnose and fix them you can waste days and weeks on it and this is the reason why a lot of new developers quit so in this video we'll look at different types of errors that you can run into and how you can diagnose and fix each one of them you'll also learn how to use the debugger tool in visual studio it's a really powerful tool for debugging issues so let's get started so i have purposefully made few errors in this project so i can show you how to fix them so let me show you the error all right so if i start a battle you can see that we have an error in the console and our game just crashed okay so let's click here and look at the error so this is a null reference exception it's a very common error but it's really easy to fix so this happens when you try to do something on an object whose value is not all right so we just have to find what object became and why it happened so if you click on the error down here you can see the stack trace of the error and this will give you the line that is crossing the error so here we have multiple lines but the line at the top will be the line that is causing the error so you can go to that line by clicking on this so let me do that all right so this is the line that is causing the error so one of the object in this line might be all right so here we have multiple objects we have dialog box player unit pokemon so if any one of these object is null then we'll get another reference exception okay so we need to find which one of this object was not for that we can use debugging in visual studio so let me show you how to debug first we need to add a breakpoint so if you click in this area it'll add a breakpoint against the line that you clicked okay and then we need to attach this to unity you just have to click this button and it will be attached to unity so now when we test the execution will pause at this break point okay so let's try testing so if i run the game and start a battle you can see that the execution stopped at straight point so now we can place our mouse on any objects above this line and it will show you the value of that object okay so if i place my mouse on moves it'll show me the value of moves so it's a list with a count of three so if we expand it it'll show us the value of each move okay so we have scratch cloud etc so similarly you can place your mouse on any object above this line and you'll be able to see the value of that object so let's check the value of all the objects in this line and see which one is actually normal okay so we already saw that moose is not nodded pokemon installs are not null there is value in it and then the player unit is also not null and if you look at dialog box it's actually null so this is the thing causing the issue right we are trying to call the setmo names function on an object that is null so we found what's causing the issue so next let's look why it's not so let me just stop the debugging and i can control and click on the dialog box to see where it's defined so it's defined as a serialized field so it must be assigned from the inspector right so let's go to unity and confirm that it's assigned from the inspector so if i select the parallel system here you can see the dialog box is not assigned all right so if it's not assigned it'll be null and we'll get the null reference exception when we try to do something on it okay so let's go here and assign the dialog box so yeah errors like this are like pretty common we'd always miss to assign some fields while testing but if you know how to debug it then you can find the issue really quickly all right so let's test this to make sure that the issue is gone so let me start a battle real quick okay so yeah now we don't have that error so that's fixed so these types of errors are called runtime errors since they occur when we run a specific part of our game so another reference exception is like a common runtime error so next let me show you another common runtime error so if i go to moves and if i press the right arrow to change the selection so we only have three moves here right but if i press the right arrow again from the third move i'll get this error so let's click on this error to learn more about it so yeah this error keeps occurring continuously so let's click on one of them to see more details so this is an argument out of range exception this happens when you try to get an element from an array or a list using an index that is larger than the array size okay so let's say you have an array or list of five elements and if you try to access the element in the tenth index then it will throw this exception okay so let's click on this to go to the line that is causing the exception so this is the line causing the exception and since we know this exception happens when we use arrays or list we should check if we have any array or list in this line okay so we do have one we have a list called moose so probably this is the one that is causing the issue so let's confirm it by debugging let me add a breakpoint over here and attach this to unity and let me just start a battle real quick okay let me just remove the old breakpoint we use this to debug the null reference error so let me just remove it and you have to press continue to continue the execution so now if we select fight as you can see that this function is executed and it passed at our break point okay so if we press continue the execution is still passed at this line we can try again but yeah it still get passed at this line so this is because this function is called from update so it will run multiple times per second so it's hard to debug things that are called from the update so what i'll do is i'll remove this breakpoint and continue and then i'll recreate the issue from here and after that i'll add the breakpoint and debug so first let me recreate the issue if i try to go to the fourth move we'll get the issue and now we can add the breakpoint and check what the issue is okay so remember whenever you are debugging something that is called from the update recreate the issue first and then add the breakpoint all right so let's check what's happening here so if i place my mouse on the moves list you can see that it has a count of three there are three moves so next let me check what index are we using so the index that we are using is three but we can't use that right if a list has three elements its index will be zero one and two right index will always start from zero so we have to make sure to limit this index between zero and two so let's look at the code that is modifying this index so when we press the right arrow key we are checking if the current move is less than the count and then we are incrementing it so if this is two then we'll increment it to three so we can easily fix this issue by adding a minus 1 over here right so now if this is 2 then we won't increment it okay so remember when you get an argument out of range exception check for arrays or list in your line and then check if the index is going out of range okay so let's just fix i'll just hit continue yeah we actually have to remove the break point and then hit continue otherwise it will keep hitting at that break point right so now we shouldn't have that error let's actually stop and play again all right so now if i try to go okay we still have that error let me check what's happening all right we forgot to save our change so this is not you'd save so let me press ctrl s and save it and let me test it again okay all right so now if i press right arrow from the last move we don't have the error anymore because we are clamping the index properly so we looked at two errors both of them were run time errors so next let's look at an example of a compile time error compile time errors are the most easiest zero to fix so let's say i want to change the name of my enemy unit object to something else like four unit by the way let me just stop debugging we don't need that anymore all right so we change the name of our enemy unit so now if we go to unity we'll have these errors and our scripts won't compile right that's why it's called compile time errors and if you try to run the game it'll show this message so these errors are really easy to fix because unlike runtime errors visual studio will show us what is causing the error okay so your scripts will have lots of red lines showing you that there's an error in the code we don't have to do debugging or anything to find out the error by the way if visual studio is not highlighting these errors then that means your intellisense is not set up correctly usually intellisense will be automatically set up while installing unity but if it's not working for you then i'll put a link in the description showing how to set it up okay so let's go ahead and change the name of our variable back to enemy unit okay and now all the errors should be gone so now let's look at the third type of errors so this type of error is where you get an unexpected behavior but you don't get any errors in the console okay so let me pause the video and create few of these errors and then i'll get back to you and show you how to fix it okay so i've made few errors so let me go ahead and show you all right let me start a battle so yeah here we have a weird error actually we have two errors so the first one is our enemy bulbasaur is just a white cube it's not showing its sprite correctly right the second error is the name of our pokemon is empty okay but if you look at the console we don't have any errors shown right so these types of issues are issues in which you get an unexpected behavior but you don't get any errors okay so since we don't have any errors it's a bit more harder to fix this because we don't know the line that is causing this issue right so to fix such errors we have to use the power of debugging so let me show you what all we can do in debugging so since this error happened when we start the battle i'll add a breakpoint at the start of the setup battle function and then we can debug this function line by line okay so let me go to unity and play the game and start a battle all right so the execution passed at our break point but unlike before we don't know if this is line that is causing the issue so we should debug this function line by line and find the line that is causing the issue all right so do that we have few options over here so these three buttons can be used to control the execution okay so right now the execution is passed but i can go to the next line i can actually execute the next line by pressing the step hour button okay so this is step over this is step in and this is step out so if i select step over it'll take me to the next line all right i can click on it again and it'll take me to the next line okay but what if i wanted to go inside a function instead of going to the next line so for example let's say i want to go inside enemy unit.setup instead of going to the next line for that we can use step into okay so we can't go back once we execute a line so we can't execute in the reverse order we'll have to continue and start the battle again to reach that line okay so let me do that real quick all right this time i want to go inside the setup function so first i'll click on step over to go to the next line and from this line i'll click on step into to go inside the setup function okay so now we can use step over as usual and execute the lines that we want so the one issue that we had is sprite of our enemy was not shown correctly right so we are inside enemy unit dot setup and in here we can check if the sprites have value assigned to them okay so if i place my mouse on the back sprite you can see it has value but if i place on the front's right you can see that it is not okay so this is the reason why we don't have sprite for our enemy so by using debugging we can execute our code line by line and find out what's causing the issue okay by the way i haven't explained the step out button right we use these two but we haven't used step out so if you step out it'll take you out of the function all right so step in will take you inside and step out will take you outside so by using these three buttons we can execute our code line by line so this is really useful in understanding how your code is running and you can understand what's actually happening then you get an issue so now we know our enemy pokemon did not have sprite so let's go to pokemons and check if the bulb also has a front sprite so yeah it doesn't have the front sprite so let me assign it and now if you test we shouldn't have the tissue all right we can just continue and we don't have that issue anymore so we still have the second issue which is the name of our pokemon is blank so let's try to solve this by using the methods that we learned so i'll have to stop the game and run again to hit this breakpoint again okay let me start a battle all right so so the data of the player pokemon is set to the hud from this function right it'll be set from player dot set data so we can try going inside that function i'll go to next line and from this line i'll press step into to go to that function okay so here you see something weird happen so this is because when you step into it will not just take you inside functions but it will also take you inside properties okay so playerunit.pokemon is a property and it took us inside that property right so if it takes you inside something that you don't want to go to you can always step out and then press the step into again so this time it took us to the set data function as we want so now let's check the value of pokemon.name from here so yeah you can see it is empty so that's why we are getting the issue and by the way we don't have to always debug line by line if you have an idea that this function might be the one that is causing the issue then you can always add a breakpoint over here instead of adding breakpoint at the start and then you can and then when you run this the execution will stop over here so you don't have to go line by line so let me just stop debugging and go to my pokemon scriptable object to check if the name is not and yes the name is not right so let me give a name to it and now everything should work correctly okay so now our charmander has name so that is fixed so let me show you one more issue of this type so i see this issue a lot in the comments so let me just recreate it and show you how i would debug and fix it so let me change my code and create that issue first and then i'll get back to you alright so i have created that issue so let me show you what it is so when i run the game my character is not working when i press the arrow keys okay a lot of people have faced this issue when starting the series so let's try to debug this so first i'll go to my player controller script since that's a script responsible for moving the player and then i'll go to the move function all right and i'll check if the move function is called so let me add a breakpoint here and if the move function is called the execution should reach this breakpoint right so let me just attach this to unity and test okay so now when i press the arrow keys the execution is not hitting this breakpoint okay so now i know that the move function is not called all right so let's check from where we are calling it we are calling it from here so maybe this if it's not working so let's add a breakpoint on the if and let's try moving the character using the arrow keys okay so even now it's not hitting the breakpoint so maybe this if it's not working so let me add a breakpoint on this if and see if the execution hits it so it's not even hitting that if so maybe this if might be the one that is causing the issue so let me place a breakpoint over here and as soon as i place it the execution hits the breakpoint right but if we remove it and place a break point over here the execution won't hit it all right it won't hit it even when we press the arrow keys so there is some problem with this if condition so let's check what we are doing here so we are trying to move the player if the player is already moving okay that doesn't make sense right we should try to move the player if the player is not already moving right so this should be not and now if we test the game the execution will hit this breakpoint and if i press continue it will also hit this breakpoint so let me also remove that and yeah it's working it won't hit this breakpoint because we are not pressing the arrow keys yet so let's go ahead and try that okay so when i press the arrow key it went inside this if also and now if i press continue again it'll even go inside our move function so let me remove all the breakpoints and the character should move properly right now so yeah it's moving properly all right so this is how you debug issues in unity so the next time you get an error in your code try doing this you might find it difficult at first but as you practice and fix more and more errors you'll become really good at it so keep practicing and as i said at the start this is one of the most useful skill to have if you are a beginner so yeah i hope you found this video useful before you leave make sure you leave a like on the video and consider subscribing to my channel and i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 531
Rating: undefined out of 5
Keywords: debugging in unity, unity error, unity null reference error, unity fix issues
Id: al2LjGRRcu8
Channel Id: undefined
Length: 25min 16sec (1516 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.