In this lecture we'll talk about debugging a Unity script using MonoDevelop. So sad to say, sometimes your games will have bugs in them. And the only way to fix those bugs is to debug, get rid of the bugs. And so, that's what we're going to talk about today. Before we actually go through the mechanics of doing that, let's talk a little bit about how debugging works. So here's how it works. We find an error in our code, a bug, somehow it's not behaving the way it's supposed to behave. Before we go messing around in the debugger and sort of watching how our program executes and so on, we should form a hypothesis about what could cause this error. Because especially once we have lots of code, sort of mucking about, executing one instruction at a time is going to take a long time. So you should form some idea about what might be going wrong before you go try to find it. So formulating a hypothesis. Now I'll freely confess that sometimes as a programmer I don't do that. I go sort of wander around in the debugger, but that's a stalling tactic. It's getting me working on formulating that hypothesis in the background while I do something else. I mean, I suppose could go read a book or go for a run or something, but it's doing something while I'm trying to form a hypothesis. Once you form a hypothesis, you go and gather evidence. And we actually can't prove hypotheses to be correct, but let's not get technical about those kinds of hypotheses. Once you sort of figure out what you think is going wrong, then you use the debugger to try to discover if that is in fact the case. And then, you sort of walk through the code, and you look at values of variables and so on to see if your hypothesis was correct. And you might discover it was and then you can fix the bug. Or you might discover it wasn't, but that's okay too because you find a different place where the problem is actually occurring. So let's actually go to Unity and MonoDevelop now to see how that debugging process works. To debug our script, we need to open up both our project in Unity and our script in MonoDevelop. And in fact our first step is to do something in MonoDevelop, so let's go there first. So I've added a little extra temporary debugging code so we can see how the debugger works in a little more detail. One of the important tools we have when we debug is something called a breakpoint. So if I click here, to the left of line 16, I get a little red circle and that line gets highlighted. And what that means is I have set a breakpoint at that line. And what that means is when I run the code, when it reaches this point, it will stop, it will break execution. And then I can sort of look around at stuff here in the environment. So we'll do that in a moment. The way we start debugging, here in MonoDevelop, is we go to that top menu bar and we select Run > Attach to Process. And in this pop-up, there should only be one Unity Editor process. If you see multiple Unity Editor processes, you have multiple instances of Unity running. So you should shut them all down and start over, otherwise it's hard to pick the right one. So just double-click it, so now we can go to the editor and start up the game. We start the game as usual by clicking the start button. And it takes a little longer to get started when the debugger is running. And you'll see that it is highlighted, MonoDevelop down here in my taskbar, so I'll click that and it has stopped here on this line. And you can tell it is stopped here on this line because that little red circle has an arrow in it, and the line is all marked with yellow. Now one of the interesting things we can do with the debugger is we can look at the values of variables as we go along. So I'm going to actually step over or execute this line of code. And then we'll take a look at the value of the position variable, which right now is 000, whoops, I hovered over the variable. To step over you can come up here and click Run > Step Over. Or you can remember that it's F10 and just press F10, but I stepped over it. The value of position here is still 000. You can also look at position down here in the Locals pane. Sometimes you might have Watch selected, like that. You can click Locals to look at the variables. And position is 000 down there as well. I'm going to step over one more line of code, which changes the x component of that vector 3 to 1. So I'll F10 to step over that line of code and you can see that position has changed to 1.0, 0.0, 0.0. And then when we're all done, we can just say Run > Stop and then we're all done. And we always need to go back to our Unity game and even though we stopped debugging in MonoDevelop, we have not stopped running our Unity game. So you just click the button again to stop running the game. And that's how you can debug a Unity script in MonoDevelop. To recap, today we talked a little bit about the process of debugging. And we also talked about how specifically we fire up the debugger using Unity and MonoDevelop.