Monster shadow

VIDEO

You NEED to know this! - Master debugging in Unity and; C#

Description:
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. ========= SUBSCRIBE: https://bit.ly/3eqG1Z6 DISCORD: https://discord.gg/GqeHHnhHpz MORE TUTORIALS: https://www.youtube.com/tarodev
ADD A COMMENT

Dan Pos - Game Dev Tutorials!
"This will blow your tits off" - nearly spat out my tea. Bravo!
OatQuaker
As a software developer, I'm surprised that people even develop a game without a debugger. How often tests are used though? Acceptance of course is part of the game test run, but what about integration and unit tests?
Tarodev
@Frank Tinsley this is absolutely my reason. In a business application the goal is generally to accept an input and receive an output. You may add performance as a secondary factor sometimes. But in game dev systems you may have an idea of what you need to accomplish, but it could take 50 iterations and changes before it 'feels' right. There is no 'feel' in business apps so as soon as the algorithm is written you can write a test for it.
Frank Tinsley
@Tarodev isn’t that because games often involve a lot of weird systems that need to be changed up drastically all the time so any tests you write are very likely going to be thrown away before long?
Tarodev
A strong lack of unit tests in game dev (at least to my knowledge). I'm even guilty of writing too little unit tests in my games, which is weird as I write them for everything in my professional life.
Martin Kašpar
Nice video, but how about debugging android games on phone? I'm not able to connect to my phone via Unity and Visual Studio.
Tarodev
There are a few extra steps involved in that, but it is possible! Ensure your phone is in developer mode and connected via usb. Alternatively you can connect it via WiFi, but it requires usb initially. Look up 'adb logcat'. But any way you spin it, Android development is a more annoying than standalone development. It's made easier by using an Android emulator instead of a real device. Strongly suggested
Kiing C:
Probably never gonna use but bookmarked. looks so useful
Kiing C:
looks like a pain to setup lol. i prefer the general way
Tarodev
If you plan to be a prigrammer, you'll most certainly use debugging sir
Mo Oumari
Amazing video as always
Kevin Evason
Your videos are some of the absolute best I've seen, and the topics you cover are just what I'm after. Thank you! <3
TheSlapDoctor
Yo I subscribed because I like your videos and all, but the clickbait manner of the titles is pretty irritating.
TheSlapDoctor
@Tarodev ahh yea I get it, sure it's incentivized. your content is pretty good tho so don't think you need to rely on it too much brother
Tarodev
Wow, I changed it to click bait last night and somebody already complained, lol. I'm just going to keep it this way for a few days to see how much difference it makes. Algorithm experiment.
Brian Purdy
Another great video Tarodev. It is always good to get a refresher on things and this is invaluable to anyone who did not have a full grasp on debugging at this point. Keep them coming, Cheers!
Qwerty Ram
Not sure if these are still worth to watch when I have switched to Godot, but these more general programming tutorials seem fun
Qwerty Ram
@Tarodev People say you shouldn't wait for Godot, I think it's some inside joke or something
Tarodev
@Qwerty Ram Sounds like I should wait for that!
Qwerty Ram
@Tarodev I think it's biggest drawback currently is... the upcoming 4.0 version. It will add so many useful things, especially for 3D, and refine existing features to the point where it could be a solid competitor to other engines, but... Well, it's still in development, not even in alpha.
Tarodev
@Qwerty Ram I'm yet to try it out. Might experiment for a week at some point and make a video on my experience.
Qwerty Ram
@Tarodev I'm not surr yet, it does seem a bit easier to just make something. I switched when I saw it mentioned again, and after seeing its python like language, I was sold...
Виктор Курбатов
🔥🔥🔥
Diru Diaz Rusiñol
Exactly what I was looking for once again
Arda Özler
Thanks my tits have been blown off
MadLibberator
Your magic trick did, in fact, blow my tits off
Tarodev
I hope you can locate them
MadLibberator
5:40 I was like, I don't have an 'eleven' key 😅 Good tutorial! I usually do the Debug.Log workflow but I'll have to use this more often
Tarodev
Oh damn haha. My bad!
NarkoPraxis
This was a great reminder.... Somehow I got out of the habit of using debug mode.
NarkoPraxis
@King Bling I wish... Really it means that for a time I was forced to develop in an environment that didn't have live debugging and now I forget it exists...
King Bling
Which means ur code is always perfect? You won but at what cost?!!
olivero Lavina
vae.fyi marvelous
SEE ALL COMMENTS


Transcript:

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