You NEED to know this! - Master debugging in Unity and; C#
There is nothing exciting about debugging, but it's such a vital tool for any developer. Train your debugging skills now and you'll save an unfathomable amount of hours searching through your code trying to find bugs.
MORE TUTORIALS: https://www.youtube.com/tarodev
becoming a pro debugger is crucial for productivity and your sanity this is simple stuff but you'll love me a long time once this video is done okay so i've got a scene here and i've got these two objects with scripts on them and there are errors in the code so let's have a look at the first script shapes so all this is meant to be doing is taking a shape it will iterate uh for as many times as our shape count is set and it will just spawn the shape and then it will add it to this shape list here so let's see what happens when we press play the variable shape prefab of shapes has not been assigned now i'm sure you have seen this a billion times but basically all this ever means in unity is that you have not filled one of your uh variables in your scripts in the editor so we can easily fix that by just sliding that in right there cool let's press play and see what happens now all right null reference exception okay so this would be the most common error uh that unity devs receive and to be honest uh it might even be the most common error that uh devs in general receive so let's see how to fix this and by the way while we're here this is not just read only you can double click this and it will take you directly to the offending line okay so it is telling us that something in this function is uh null it's and it's throwing an exception so what you could do is go in here maybe and debug log you know uh let's debug log the shape see if the shape is null or whatever but let's try not be noobs for a second let's remove this and do it the proper way so we know that something in this function is throwing an error so what we can do is create a break point on the first line of this function now i'm using visual studio but it is exactly the same in vs code and brighter the only difference is maybe the hotkeys might be a little bit different in brighter but functionally it works exactly the same so now that we've got this break point here what this is going to do is unity is going to execute this code until it hits our break point and then it's going to stop and let us inspect stuff okay so to begin debugging we just press this button up here attached to unity if you don't have this it probably means that you have not installed the tools for visual studio for unity so go to the unity hub and just add that module so press the button here or just press f5 and that will start uh in debug mode now back in unity let's clear this if we press play now it will now execute our code and stop at our breakpoint google bananas so now when we're in debug mode we can actually hover over here and we can see the actual variable uh settings we can see what they're set to so we can see now we actually have a prefab set to this shape prefab we can see that our shape count is set to three cool so what could be null here what's throwing this exception is it our shape no shape seems to be good is it our shape list yes it's null we have not initialized our shape list anywhere so let's fix that let's do it up here before we start actually setting stuff let's say our shape list is equal to new list game object cool so with our breakpoint still selected let's attach to unity again and let's clear this and then try again so now if we inspect it we'll see that it has indeed been initialized and it's got a count of zero now that we have fixed our bug what if you want to continue stepping through the code to inspect what goes on you can come up to here and press this little button here step over alternatively you can press f10 now i highly recommend learning these hotkeys as there are only a handful of these debugging hotkeys but they make your life so much easier so let's press f10 and as you can see it goes to the next line and it's indicated by this arrow here now if we hover over shape list we'll see that one shape has been added to our shape list and we can actually click this and inspect our list we can see at index 0 here we've got this shape that was just added so let's remove this break point here and as we're at the end of this function here if i press f10 it will actually break out of this function and it will go back up to our loop and as you can see our i is set to zero but if we jump into this loop now it will now be one cool so that's how you step through code one little caveat is that pressing f10 will not actually step into a function so i'll demonstrate that by just going pressing f10 going down to this line here which is the function call here if i press f10 again it will not actually show us what has happened there it will just execute it and then send is next to the next line if you want to actually jump into the function or step into the function you can come up here and press this button here step into alternatively you can press f11 and it will jump into that function now now it will happen uh very often that when you're debugging your code or god forbid somebody else's code you will accidentally jump into a function that could be like a thousand lines long and you know that function works and you know the error is not there and you just want to get the hell out you don't want to press f10 a million times so what you can do is you can come up here and press this button step out or press shift 11 and that will step us out of this function and it will allow us to continue the execution here cool so i'm done now i know that this script is working um so i've i've unticked all my break points and now i'll just click continue and that will just continue the execution like normal and as you can see we have now fixed our script and it's spawning three cubes excellent so that's one out of two one out of two so let's disable that and let's now enable our numbers script cool so let's have a look what numberscript is doing well we're creating a new variable here called num and we're setting it to start number whatever this start number is set into in the editor we're iterating for as many as iterations is and we're multiplying our number by the multiplier and then we're coming down here and we're dividing by our divider and then we're printing the final result so let's see what happens if we press play infinity uh-huh well that doesn't seem right so let's set a breakpoint and have a little looksies press play all right so number will be zero but after we press f10 we'll see that it will be set to three because that's what start numbers set to we've got iterations here for three and we'll say num is three multiplier is five if we step down it's now being multiplied multiply it again multiply it again and that's three iterations so on the next one it will go out and now let me show you a magic trick all right this will blow your tits off so we're down here right we have just uh executed our code uh as we've written it but visual studio allows you to actually grab this break point here and pull it up and we're basically saying could you please run this section of code again it's not reversing time because if you inspect our num here it's actually the the multiplied version of our number so we're actually controlling the flow of of this code here so this is super handy that if you're stepping through stuff you may have missed something so you can pull it back up and try it again also you can actually change variables on the flyer look at this sorry i'm hovering over num here i can actually change this let's make this 150. so we've actually changed this at runtime amazing way to try different edge cases and not actually set the game up so that these variables will be these uh perfect things for the edge case you can actually just change them directly in the editor so let's go down here again this will be three iterations cool now down here our number is getting pretty big and our divider is zero and if you know anything about dividing by zero you'll know bad things happen and that would explain why our number is coming out as infinity we know now that our divider so obviously this was a very uh obvious thing uh but when you're writing your game algorithms and stuff this will be an invaluable tool in your tool set and it will save you many many tiers your many mentors so if we press play now we will see that it does indeed give us a correct answer excellent so if you learned something or if you liked the video leave a freaking like boys like the video and uh subscribe as well because i'll be releasing more of this juicy contents and i'll see you next time you