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 loops
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to learn how to use the for and while loops. All right. So in our Scripts folder, let's create a new C# script, and let's call this Lesson_09. And we're going to open that in MonoDevelop. And then we're going to take the script from Lesson 8 and we're going to copy that into Lesson 9. So I'm going to hit Control a in Lesson 8 and then Control c. And the Lesson 9, Control v. Let's make sure that we rename our class to the proper name, so Lesson 9, and then hit Save. All right. So to get started here, let's go below the Void Display message function and let's create a new one. So I'm going to call this void EnemySearch. OK. Open, closed parentheses, and then we're open curly brackets, hit Enter two times, and then close curly bracket. And I'm going to hit the arrow key to go in between those curly brackets and start typing in my for loop. Now, a for loop is going to allow us to loop a block of code and a function for a certain amount of times that we specify. So for is what we need to start out with, and then we're going to open parentheses. And we're going to give it the conditions that it needs, or the three parameters that it needs. So we need to initialize the variable, then we need to give it the condition, and then we need to tell it what to do if that condition is met. So the initialization is going to be int, and then we're going to use a generic variable that is used very commonly in for loops. So that's going to be i. And we're going to set this equal to zero. OK. Now we, need to give it the condition, and we're going to use i is less than five. We want it to increment plus one. So i plus plus. And there we're going to end with parentheses there. Now, one thing that I want you to notice-- and it's very important. In the for loop, between those three parameters, we have semicolons. So in our initialization, we have i equals 0, semicolon. i is less than five, semicolon. And then i plus plus. So in a for loop, we open curly brackets and then we close those curly brackets. And let's go ahead and type in the code that we want in our for loop. So in my for loop, I'm going to go ahead and give it an equation here. So I'm going to get EnemyDistance. And I'm going to set this equal to a random number. So we're going to use that random.range. And we're going to set that range from one to ten. OK. Let's go ahead and initialize our variable. So up here at the top under the Private string, we're going to go ahead and hit Enter two times. And I'm going to type in Public. And actually, I'm going to go ahead and make that a private variable. So private EnemyDistance. And I'm going to set that equal to zero. OK. Now, I need to make sure that I give it a data type, and that's going to be int. There we go. Let's save that. And let's go ahead and go down and let's finish off our for loop here. So still in the for loop, we're going to create some if statements. Now, we're going to create single if statements. We're not going to create else or if else statements and things like that. So the first one is going to be if, and let's give it the condition. So if EnemyDistance is greater than or equal to eight, let's print out a message here. So debug.log, and I'm going to say an enemy is way over there. And I'm going to end that if statement. Let's start a brand new if statement. And we're going to say EnemyDistance. So you can just hit Enter to enter that very quickly. And we're going to say if EnemyDistance is greater than or equal to four and EnemyDistance is less than or equal to seven, we want it to do something else. So we're going to say debug.log, and we're going to say that enemy is getting closer. End the curly brackets, and we'll do one more if statement. So if EnemyDistance is less than four, we're going to do a debug.log. So debug.log. This enemy has terrible breath. OK. And then we need to end our for loop. All right. So, actually, I already have my for loop ended, there. So if you want to, you can check those parentheses. Just make sure that they match up-- or I'm sorry-- Those curly brackets. So everything is matching up here. Now, I want to give you a little bit more explanation on why I'm using these if statements the way I am. With the if and the else if statements, what will happen is it will check that first if and then it will iterate through the rest of those. And I don't want it to do that. I want it to loop through, and whenever it hits one of these, it stop it goes back. I don't want it to keep going through all of these. I don't want it to check to see if this one's true, and then I don't want to check to see if this one's true. I just want it to find one and then go back and continue through the for loop. So now that we have that, let's go ahead and save this, and let's go into Unity. And let's apply that Lesson 9 to our game object here. So let's Remove Component, Lesson_09. And we're going to hit Play. And if we hit the Space Bar, you'll see that it says you found the axe. But it's not giving us the EnemySearch. And that's because we didn't actually call that function. So what we need to do is we need to come back up here to our input, and we're just going to say EnemySearch. Open, closed parentheses and then semicolon to call that. Let's go into Unity. Let's Play. And we'll hit Enter. And let's open this up. And what you'll see is that we set our first action, which is our weapons search. It says we found the bow. And then it goes through and loops five times to search for the enemy. So it's saying getting closer, way over there, getting closer, this one has terrible breath, and that one's getting closer. All right. So this is how we can use a for loop. And again, a for loop is a way for us to loop a specific set of code for a certain amount of times that we specify. All right. Now, let's move on to a while loop. Now, a while loop works a little bit differently. A while loop will loop as long as a condition is true. All right? So let's go in and let's create a while loop. So to get started with this, let's go ahead and comment out our for loop, here. So I'm just going to forward slash, hold Shift, and then eight. And let's go down to our for loop quotations. And we're going to hold Shift, eight, and then forward slash. OK. Now, above this-- let's go ahead and just give ourselves a little bit of room. Let's type in a simple while loop. So the first thing that I need to do in a while loop is I need to initialize the variable that I'm going to be using. So I'm going to use that i variable again. So with this, we're going to type in int i equals zero. And this is going to look familiar because the for loop just looks a little more compact than a while loop. So we have this initialized. Let's go ahead and type in while. And we're going to say i is not equal to five. Then do this. So let's go ahead and open curly brackets and close curly brackets, and let's go ahead and tell it what to do. So we're going to say i plus plus. And the i plus plus is extremely important in a while loop because a while loop can become an endless loop, and that can break your game. You always need some sort of way out of a while loop-- some sort of condition and a way to fulfill the while loop condition here. So then we're just going to do something very simple. We're just going to say debug.log, and we're going to tell it what to print out. So the variables that come from i. So this is a very simple while loop, and again, it's very, very simple to a for loop. But this one, we could actually have it go for a lot longer, and we could put in different variables in here. So we could say while a character is not grounded. We could say that he's alive or something like that. While a character is alive, the enemy state must keep running, and that sort of thing. This is just a very quick example for this. So let's save this. Let's run our code. And we've got some sort of warning here. It's saying private field enemy distance is assigned, but its value is never used. Let's go ahead and double-click on that, just to see what it's talking about here. Oh, OK. The enemy distance is not being used, so let's go ahead and just comment that out for right now. And let's go into Unity. And that clears itself up. Lets hit Play. And we'll hit Enter. And you'll see that we found the sword and that it iterates through that while loop. So we can see one, two, three, four, and five. Once that condition has been met, it stops going through that while loop and stops executing that code. All right. So now that we have taken a look at for and while loops, we're going to go into our next lesson, where we're going to start talking about arrays.
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