Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Creating logic with if statements
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we're going to learn how to use if statements and how they can be used to create logic. So let's create a new C# sharp script, and let's call that lesson_06, and we'll double click on that to bring up MonoDevelop. And I'm going to go ahead and start fresh here because once we start getting into if statements, we're creating logic. And when creating logic, you have syntax, or the way that you write your code, can become a little muddled and can get really confusing at times. And it gets really dirty and you can't really debug that sort of code, so I just want to go through and just start to show you my technique for creating clean code. So at the top of my C# scripts, I always have my name spaces at the very top, that way I can always know exactly what I'm pulling from wherever I'm creating all of my different codes. So then, we'll have our classes, and everything is going to belong in our class. Now, what I like to do is I like to make sure that my curly brackets, whenever they're open, that they are at the left side of my script here. I don't really like them at the end of my class here, I find that very distracting because I'm looking through, and I see my closed curly bracket, but I don't really know where the open one is, and then I have to kind of look off to the right. So I like to just hit Enter one time and put that one line below so that way I know one is open and one is closed. Same thing with my functions. Now, you'll notice that whenever we have our cursor right next to one of these open or closed curly brackets, it will highlight in gray it's pair. So that way I always know that these are paired together. All right, now inside of my class, I will go ahead and create all of my public variables. And I like to put all of these to the far left as well, so that way I can see them really easy. So our first variable that we're going to create is going to be public, so we need to bring in our access modifier, and then we're going to give it an identifier or a name. So we're going to say speed equals and I'm going to say 0.0, and I'm going to hit F at the end of that, because this is going to be a float value. Now, we have our identifier, but we did not put our type for our value. So we need to type in float right before our identifier. OK, so we need that F at the end of our float value, so that way it recognizes that that indeed is a decimal value. Let's go ahead and go to our next one, we're going to type in public float, and we're going to do distance equals, and I'm going to type in 210.0f. And we'll go ahead and get our next one, float time equals, and I'm going to say 3.0f and end with a semicolon. And then, two more. Public float max speed limit, and we're going to set that equal to 70, and we forgot to do 0.0f. And we'll type in one last one, which is going to be our minimum speed. So min speed limit equals 40.0 and then, again, make sure you add that f there. So here, I can see all my different variables, and I can access them very quickly if I need to find them again. And then, I also like to group variables together by commonality. So when these are dealing with the speed limit for my script here, and let's say that I have something that's supposed to deal with traction for the car or something like that, what I'll do is I'll create a new block with new variables. So I like to just keep them grouped. Now, I don't necessarily have any variables for that, so we're just going to leave it just the way it is right here. So now, what I like to do is I like to come down into my update function, or whatever function I'm going to be writing in, and start working with my different statements. So to get started here, inside of our update function, I'm going to go ahead and tab over one time and we're going to create our speed equals distance over time statement. And let's start putting in our if statements. So if, notice how this is tabbed over one from our curly brackets here. This just helps me keep things nice and clean. If speed is greater than max speed limit, then we're going to do something. All right, so with if statements, we've already tasted them a little bit already. We've already seen them and we've learned about comparison operators as well as logical operators in an if statement, and we're going to go ahead and we're going to continue on with this. So an if statement requires a condition. So we have if, and then, in parentheses, we have our conditions, speed is greater than max speed limit. So if this is true, we need to tell it to do something, so that's going to be inside of our new curly brackets here underneath our if statement. So if, speed limit is greater than max speed limit, we want it to print out a message. So debug.log and then, give it its parameters of what to print out, and we're going to say you are exceeding the speed limit. And then we end that statement with a semicolon and then we have to close our curly brackets for that if statement. Let's save this, and then we're going to go into Unity. Let's apply our lesson six script here, so I'm going to right click on this component, Remove Component. Let's drag and drop lesson six, and let's hit Play. So our speed is currently set to 70. We're not breaking that speed limit, so we are not getting any message. So if we adjust our time, we are now over that speed limit and now, we are getting that message. So I want to give some feedback on whether or not we are breaking the law or not, whether we're going over that speed limit or not. So I can do that by creating an else statement. And an else statement is kind of like the default action that you should have whenever looking at this condition. So if speed is greater than max speed limit, it's going to do this. So we want to tell it what to do if it's not greater than the max speed, so we're going to use an else statement to do so. Now, else statements do not need a condition because they look at the if statements above it. So else, and then we'll open curly brackets, and then inside of that we'll tell it what to do. So debug.log and then we'll say, you are within the speed limit. Let's end with curly brackets here. And now, let's save that and take a look at that in Unity. So now, let's hit Play, and it's telling us, you are within the speed limit. If we adjust our time and we go over that, it's still saying that we're within that speed limit. Let's go ahead and go over the speed limit here, and now it's saying we're exceeding. All right, now, what we want to do is we want to create some logic to tell it if we are below the minimum speed limit. So how do we do that with if statements? Well, we'll use what's called else if statements. So we have our initial if statement, and if we need another one, what we'll do is we'll say else if, and then, we'll give it its condition. So the condition is speed is less than min speed limit, we use our IntelliSense here. And then we'll close parentheses, and then we'll open a curly brackets, and then tell it what to do if that condition is met. So debug.log and then give it its message, you are not going fast enough. End that with a semicolon for our statement, and then closed curly brackets. Always pair up your curly brackets in your if statements as well as your functions. All right, so let's save this and take a look at it in Unity. So we'll hit Play and it's saying we're within the speed limit. Let's take our time up, and let's go under 40. And it's saying you are not going fast enough. Let's check to make sure, if we can get up over the speed limit, and it's saying we are exceeding. So everything is working according to plan. Now, you can use as many else if statements as you like here, so I'm going to use another one just to show this off. So else if, give it its condition, so I'm going to say speed is equal to max speed limit. And then, I'm also going to say or speed is equal to min speed limit, open curly brackets and give it the command on what to do if that condition is met. So we'll say debug.log and the message we want it to print out is saying, you are close to breaking the law, closed curly brackets. And then, we'll go ahead and hit Save. And go to Unity, and then we'll play that. And right now, it's saying you are close to breaking the law because we are at our max speed limit here. We can go ahead and take our time down, and we can get close to 40 there. So at 5.25, it's saying that we are close to breaking the law. Now, if we are just above that, let's say we're at 40 point whatever, that is not equal to 40. So it's saying you are within the speed limit here. OK, so all of our conditions are working here, all of our if statements are looking good. And we've also learned how to keep things nice and clean and organized. All right, so in our next lesson, we're going to move on from the if statements, and we're going to use a different method of creating the same type of code, but we're going to be using what's called switch statements.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.