Important Notice:

The Digital-Tutors site will soon be retired and your access will be turned off - but don’t worry, you can still enjoy all your favorite Digital-Tutors content on Pluralsight (plus thousands of additional courses, skill assessments, paths and more). You can log in to Pluralsight with your Digital-Tutors credentials, as well as update your password at any time on Pluralsight. Please contact us with questions or concerns: support@pluralsight.com.
Working with assignment and arithmetic operators
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this tutorial, we'll learn how to use assignment and arithmetic operators. So inside of Unity, let's create a new script. And let's call this Lesson_04. We're going to double click on that and bring up Monodevelop. And inside of here, let's go ahead and start listing out some of the different operators that we can use. So there are two types of operators that we want to talk about in this lesson. We want to talk about assignment operators and arithmetic operators. Now assignment operators are going to assign a value to an identifier. So we're basically saying this variable has this data or this value. So let's go ahead and take a look at that. So right up here, we're going to typing in assignment. And let's actually comment this out. So I'm going to do a multiline comment here. And let's make sure that we keep our semicolon at the end of that. So I'm going to do forward slash, Shift and then 8, do a multiline comment. And I'm going to type in here Assignment Operators. And our first one that we're going to have is equal. We've seen this one already. We're basically saying that this variable, this identifier, has this data. We have plus equals, which is basically saying x equals x plus y. We have minus equals. And it's basically the same thing. We're saying x equals x minus y. We have times equals. And you kind of see the pattern here. And then we have divided by equals. And then we also have modulus, which is the percent sign. So hold Shift and then 5, and then equals. And we'll talk about what modulus is later on. So these are our assignment operators. Now what we're going to see in this lesson is simply going to be the equal sign. And we've already seen this and how it works. So now let's go ahead and talk about the arithmetic operators that we can use. So I'm going to type in Arithmetic Operators. And let's go ahead and list those. So the first one that we have is the plus symbol. And that is simply addition. We have the minus, or the dash, that stands for subtraction. We have multiplication, so Shift 8. And we have division, which is forward slash. And then we have modulus, which is Shift 5, which gives us that percent symbol. And modulus is basically going to divide two numbers and then drop off the remainder. So that way we don't have any decimal use or anything like that. And then we have two more arithmetic operators. We have plus plus, which will basically be plus 1. And then we have minus minus, which is going to decrement value. So that is going to be minus 1. So we have our assignment operators and our arithmetic operators. So now let's take a look at how we can actually use these. But before we do, we need to close our multiline comment. So to do that, we'll hold Shift 8 to get our asterisk, and then the forward slash. And that will end that multiline comment there. And let's come in here to our update function. And I like to take the curly brackets here that you see on your functions, and I like to line them up. It just makes it easier for me to keep things organized. So inside of my void update, my updates function, let's go ahead and start typing out some simple variables here. So I'm going to show you how to use an assignment operator and an arithmetic operator all in the same lesson here. So let's create a couple of variables first. So our first variable is going to be speed. So let's type in public int speed equals. And I'm just going to assign that a value of 0 for right now. Let's type in public int, which is going to be a whole number. We're going to do distance equals 0, for right now. And then public int time. So distance over time equals your speed. So we're going to set that to 0 also. So those are our variable. Let's go down into our update function. And let's type out just a simple equation here to use our arithmetic operator. So we're going to use speed. And that's going to equal distance divided by time. So Update is going to constantly check this value. Now we have these as public variables. And they're set to 0. So let's go ahead and save this right now. Let's go into Unity. And let's go to our game object. Let's remove that lesson 3 script. So right click, Remove Component. And drag and drop in our lesson 4 onto our game object. And you'll see our speed, distance, and time are set to 0. If we were to play this, you'll see that we have a division by 0. And it's going to throw an error for us. So what we need to do is we need to go ahead and adjust those values. So I'm going to say a distance of 200. And let's say that I want to make that distance in two hours. How fast would I need to drive? What would my speed be? Now if I hit Play, you'll see that nothing's happening here. I haven't given it a way to output or give myself any feedback. So what I want to do below this is I want to type in debug.Log. And we're going to open parentheses, open quotes. And we're going to say "You are traveling at-- and then we're going to put a space and then close those quotes. Now we could also use the addition not only as an arithmetic operator, but also as a way to add in variables to sentences like this. So I'm going to say speed. So let's hit the plus sign, speed. After that we're going to hit plus again and add on a little bit of text. So we're going to open those quotes again. We're going to hit the space bar. Well, let's not even hit the space bar. Let's just get a period and then close quotations, close parentheses, and then a semicolon. Let's go ahead and save this. Let's go to Unity. And we'll hit Play. And once we play that, you'll see here it says, you are traveling at 100. And we can see that here, and our speed here. If we wanted to modify that even more, we could say MPH four miles per hour. Give that a space there. Go to Unity. Play. And you'll see that that has now changed. So here we've seen a way that we can assign values or data to our identifiers. And then we can also use those operators for arithmetic to create equations. All right, so now what we want to do is we want to start getting into comparison and logical operators. And we'll talk about those next.
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