Important Notice:

On July 31st, 2017 the Digital-Tutors library will be retired along with Digital-Tutors concurrent licensing. Effective August 1, 2017 any active group plans will be redirected to www.pluralsight.com. Each group plan will receive one individual user license on Pluralsight for each active Digital-Tutors license. Please contact your account representative for any additional questions.
Working with comparison and logical operators
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this tutorial, we're going to learn how to use our comparison and logical operators. All right, so in Unity, let's create a new script. So we'll right click on our Scripts folder, and we'll create a C# script and let's go ahead and call this lesson _05. And I'm going to double click on that to bring it up into MonoDevelop. And then I'm going to take lesson four's script, and I'm just going to copy all of that, so Control-C. And then in lesson five, I'm going to replace this by hitting Control-V. All right, and let's make sure that our class has the correct name. Now remember, classes must be the same name as the actual script's name. So let's go ahead and type in 05 there and then we'll hit Save. All right, so to get started here, let's list out our comparison operators. So I'm going to type out Comparison Operators. And let's start listing those out. Now comparison operator is going to allow us to compare variables to other variables or variables to values, OK. So a comparison operator. The first one or the most common one that we'll be using, is going to be "is equal to." OK, so if you need some help to remember what that is, you can always just type that in. So, "is equal to." So if we're saying this value or this variable is equal to this variable, OK, then we'll use some sort of "if" statement or something like that to tell it to do something if that equation or if that comparison is true or false. The next comparison operator that we have is "not equal to." OK, so "not equal to." The next one that we have is "greater than," and we know that one. And then we have "less than," which is its opposite. We have "greater than or equal to," and then we have "less than or equal to." OK, and you can go ahead and write out what those mean next to those if you'd like. Let's go ahead and move on to our next set which is going to be our logical operators. Let's go ahead and just get these out of the way. So let's list these out. OK, so logical operators are going to help us create logic in our "if" statements in our comparisons and things like that. So a logical operator, the first and most common one that we will use, is going to be "and." So if we're saying if this variable and that variable is equal to this variable, then do this. So this is "and." Now sometimes, we may have a situation where we want to compare variables together, and we say if this variable or this variable is equal to that variable, then do this. So "or" is represented by two vertical lines. OK, so if you hold down Shift and then you hit the key directly below the backspace, that will give you two vertical lines. You want to hit that twice, and this is going to be "or." And then finally, we have "not." K, which is Shift-1, which will give you the exclamation point. K, now there may be instances where we say if this variable is not this variable or is not this value, then do this. All right, so now that we have those listed and out of the way, let's go ahead and put that into practice. So let's put to practice our comparison. So we're going to take a look at comparisons in a simple "if" statement. So we have our speed, which equals distance over time, K, and we're saying that we already have a debug log that says that you're traveling at a certain speed miles per hour. So right below that, let's put it in an "if" statement. Now we're going to talk about "if" statements in our next lesson, but I just wanted to show you how these comparison and logical operators work. So if, and then open parentheses, speed is greater than. So hold Shift and then your greater than symbol and then we're going to go ahead and hit, say, 70. So if our speed is greater than 70. And we're going to hit Enter one time, and I'm going to hit Shift and then open curly brackets. And then I need to tell it what to do if this is true. And let's make sure that we close our parentheses there. Sorry about that. So I'm going to go ahead to replace this debug.log here, and I'm going to say you are over the speed limit. K, now whenever we have "if" statements and we have open curly brackets, we need to make sure that we close that. So I'm going to hit Enter one time underneath my debug.log and I'm going to hold Shift and then close curly brackets. Now notice whenever your cursor is next to a curly bracket or even a parentheses, it will highlight in gray the paired parentheses or bracket, K. Each parentheses or brackets must have a pair to it, K, for this to work properly. So if we select next to this one, you'll see that these two are a pair, and these will open and close this update function. And then this parentheses here will actually open and close the class. So now we have that, let's go ahead and save this, and let's go into Unity. And we're going to remove that monoscript behavior there. So I'm going to remove component, and then I'm going to attach to lesson five on to our game object. Now we have speed, distance, and time here. Let's go ahead and give them a value. So I'm going to go to 100 miles on my distance, and let's say that I want to take one hour to travel that distance. Let's go ahead and hit play, and you'll see that our speed is 100 miles per hour. So that is over that speed limit and saying here you are over the speed limit. OK, now if I were to take that time up, what would happen is this will stop running. So if we take a look at our console, you'll see that it's no longer running that speed limit script. K, now we have a bunch here because it's in that update, but it is no longer running. Let's go ahead and take that time back down to 1. And let's just type that in. And you'll see that this begins running again, OK, because it is true. Let's go ahead and close that and stop our game. And let's go back to Unity's MonoDevelop. All right, so we've taken a look at a comparison operator and how it works in an "if" statement. Now let's take a look at a logical operator in an "if" statement. So to create logic, let's say that we want to create a minimum speed limit and a maximum speed limit, and we want to tell the player if they are under the speed limit or if they're over the speed limit, we would have to create what's called a range. So to do this properly, let's create two new values here or two new variable. So I'm going to type in public. And I'm going to say max speed, and we're going to set that equal to 70. And let's type in public min speed, and we're going to set that to 40. So how do we write this? Well, we're still going to keep our speed equals distance over time. Let's go ahead and replace our "if" statement here. So our "if" statement says if speed is greater than 70, and then we'll type in "or." OK, so the double vertical lines there. If speed is less than 40, K, we want to say that our debug.log, we want to say that you are not within the speed limit. OK. So let's hit save on this. Let's go into Unity. And it's saying here that we're having a problem. We've got an unexpected symbol. So let's go into debug here or into here and let's take a look at what's going on. Public. Oh, we forgot our type. OK, our data type. So we need to type in int on both of those. Now let's hit save. Let's go into Unity. That should clear. Let's go ahead and go into our console here and clear that, and then we'll hit play. And right now we're running at 100 miles per hour. so let's say that we're not within that speed limit. Let's go ahead and take our max speed and let's adjust just that above 100, OK. Now, you'll see here is it that max speed is being changed, K, but we're still not within that speed limit. So let's take a look at what's happening here. Why isn't this stopping? So let's hit play and let's take a look at our logic. Now normally, whenever code is working and everything is written properly in syntax but the game just isn't working properly, you know that all it is is just a simple logical error, OK. So everything's running correctly. There's one thing that we didn't do. We didn't change these values to the variables we wanted to use. So we're going to replace 70 here with those variables. So let's type in max speed, and we know that it's going to work because we have this variable that's already showing up. And then we're type in min speed. And let's go ahead and just double click on that and replace that. Now let's hit save. Let's go into Unity. Let's clear our console. Now, it will usually clear on play, but let's go ahead and hit play anyway. You'll see that we're not within that speed limit. Let's go ahead and increase that max speed limit to over 100, and you'll see that our script has now stopped running. K, that function is no longer true in either one of those cases. Let's go ahead and take our max speed back down to 70. Let's adjust our time here. And if we're at 50 miles an hour, go ahead and drag that down, there we go. Now we're within 50 on our speed, and now we can work here. So in our next lesson, we want to take a deeper look at if statements and see just exactly how they can create logic for our scripts.
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.
Introduction and project overview
1

Introduction and project overview

 
00:56
Basic C# Terms
2

Basic C# Terms

 
12:02
Creating and manipulating variables
3

Creating and manipulating variables

 
07:50
Working with assignment and arithmetic operators
4

Working with assignment and arithmetic operators

 
08:11
Working with comparison and logical operators
5

Working with comparison and logical operators

 
11:17
Creating logic with if statements
6

Creating logic with if statements

 
11:38
Creating switch statements
7

Creating switch statements

 
07:23
Creating custom functions
8

Creating custom functions

 
11:59
Working with loops
9

Working with loops

 
11:22
Understanding arrays
10

Understanding arrays

 
16:00
Project: Basic move script
11

Project: Basic move script

 
07:26
Project: Refining the movement script
12

Project: Refining the movement script

 
08:53
Project: Creating the jump function
13

Project: Creating the jump function

 
09:54
Project: Refining the jump function
14

Project: Refining the jump function

 
08:39
Project: Finishing the jump function
15

Project: Finishing the jump function

 
06:45
Project: Creating the advanced move script
16

Project: Creating the advanced move script

 
13:48
Creating the jump functionality
17

Creating the jump functionality

 
05:58
Project: Scripting basic animations
18

Project: Scripting basic animations

 
08:16