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 and manipulating variables
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we'll learn about variables and discuss how they can make our scripts more powerful. So inside of Unity, let's right-click on our Scripts folder, and we'll create a new C# script. And let's call this Lesson_03. Double-click on that and bring up MonoDevelop. And inside of here, we're going to learn how to construct a new variable. Let's go ahead and go into our public class. And let's start talking about how to create our variables. Now, there are several different parts to a variable. And the first part we're going to talk about is going to be the access modifier. And the access modifier tells who can access this variable. So we're going to have other classes and other functions out there in our game. And we may need to get the data from a specific variable in another class or in another function. And we need to make sure that the variable or that code has proper access to a variable. So if we want everyone to have access to a specific variable, we need to use the access modifier, public. Now, if we only want this class to have access to this variable and no one else, we'll use private. Now, there are three others, access modifiers, that we have available to us, but we're only going to learn about public and private. The other ones are just outside the scope of this course here. So the access modifier that we're going to use is going to be public. We want everyone to have access to this. Now, there's another important reason that we would want to use public. And the reason that we would want to do that is because this type of variable is exposed in Unity. And we could actually control this variable's data inside of the GUI, or the "gooey." So we have public. We have our access modifier. And after the access modifier, we can add in what's just called modifiers. And a common modifier that is used is const. And this stands for "constant." And this specifies that the value of the field or the local variable cannot be modified. So if we always want this variable to stay exactly the way it is, stay the same, we would use this modifier called const. Now, there are other ones that we could use as well. But I just wanted you to know that we have the access modifier and then the modifier itself. I'm going to go ahead and take that out, because I don't really need that for this specific variable. So the next thing that we need is the variable's type. So a type is something that tells the variable what type of data it's going to hold. So if I was going to hold data of a whole number, I would use int, for "integer." Now. if I wanted to have a whole data that was a float value or a decimal value, I would type in float. Now, there are several other data types out there, but we're only going to talk about just a select few. And right now, we're going to put in int. Now that we have our access modifier and our actual data type, we want to put in an identifier, or a name, for this. Now, a name could be anything that we want. So I could put in speed and we would be just fine. Now, there are some names or words that we cannot use as the identifier. These names would contradict the API, so words like while or if. Notice how these words are turning blue. If we did for or something like that, that would be a little bit of a problem. So we want to be able to use words that do not contradict that API, the automated programming interface. So I'm going to type in speed. And we're going to use that as our identifier. Now, there are a couple of rules whenever creating the identifier. The first one we talked about is, it cannot contradict the API. The second one is, it must begin with a letter or an underscore. Now, it can contain letters. It can contain numbers or punctuation only after the first letter. It cannot contain spaces. So if I were to create a variable that was two words-- let's say max speed-- this would not work for us. The way that I would write this out is I would use what's called camel casing. So max would be the first word, and it's all lowercase, and then the second word would start out with a capital letter. So maxS-- capital S-- and then Speed. So that's the way that we would write out a value or an identifier that needs two words, or two or more words, I should say. So let's just type in speed here. And now that we have our identifier, let's give it its assignment. So every identifier needs to be assigned to a data value. So we're going to type in =, and that is your assignment operator. Now, once we have it assigned, we need to give it an actual value or data. So I'm going to say my speed equals 10. So we'll end that with a semicolon. Now remember, a variable is like a statement. It must be ended with a semicolon to let it know that that is the end of our variable. Now, once we've created that, you'll also notice that inside of lesson three, this is the class-- you can see that icon there-- and then we have a variable, which is represented by this blue diamond. That lets us know where we're at on our script here. So if I were to click down here, you'll notice that we are now in this function. Let's go back to our line six here. And it's showing us that it's in class three. And then we also have that variable speed available to us. Let's go ahead and save this, and go into Unity and see what this gives us. Now let's go ahead and go to our GameObject. Let's right-click on that and remove that lesson two component. And let's drag on lesson three. Now with that GameObject selected, you'll see that we have our script. And then we also have this speed. And we can see that the value is 10. And this is an exposed variable. We could actually change this value here, if we wanted to, and run it at the speed of 20. Now, while we're playing, we could also adjust that speed just to do some debugging and things like that. But it will always go back to what you've typed in before. Now, the default value that you have will always be 10. You can see that in your script here-- public int speed = 10. So if you want to switch that back, go to your GameObject and just simply type in 10. Or you could right-click on the component and hit Reset. All right, let's go ahead and start discussing the different operators that we can use with our variables. And in our next lesson, we're going to talk about assignment operators, and then also arithmetic operators.
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.