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.
Basic C# Terms
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this tutorial, we'll learn some of the basic terms that we'll hear throughout the course. All right, so inside of Unity, go ahead and right-click in your project panel and create a new folder. And let's call this scripts. Right-click on that folder and create a new C# script, and we're going to name that lesson_02. We'll double-click on that C# object, and this will open up MonoDevelop, and it will also open up that C# object inside of MonoDevelop. Now in this object or script, you're going to notice that we have some lines of code that have automatically been created for us. And this is the template that Unity will use for you to begin creating your script. Now as we go throughout this course, we're going to hear several different terms throughout the course that are going to be very helpful for us to understand how scripting works exactly. So those terms that we're going to learn about in this lesson are going to be namespaces, classes, functions, statements, and comments. So to get started, let's learn about what namespaces are. And namespaces, in general terms, are essentially collections of types or functions and classes that are drawn from other namespaces. So, for example here, you can see that we are using the Unity engine namespace, and then we are also using the system.collections namespace. And again, this is going to allow us to use functions and classes that have already been created, and this is really going to allow us to save time and really focus on creating our game that we want. K. Now moving on, we have what's called classes. Now a class is essentially a blueprint, and this blueprint is going to show us all of the different functions that we have in a script, all of the different statements, and it's also going to hold comments. So what is a function? Well, a function is essentially a paragraph of a chapter. It's going to hold all of the instructions for this specific part of the script. So for example, if we had a player and we have a jump function, that function is going to hold the instructions and tell the player how to jump, how high to jump, how far to jump, and when to do it. Now we also have inside of functions things that are called statements, and statements are single lines of code that are simple instructions. All right, so let's get into creating a statement. What does that look like exactly? Well, we need to create statements inside of functions, and we can see a good example of a function right here. We have this void start, and then we also have this void update. Now these are two functions that are already used by Unity. And the start function is going to be used at the very start when the script is run, and it will only use that statement that is inside of that function one time. Now inside of the update function, it's going to run that statement over and over again every single frame because it's constantly updating. So inside of start, let's go ahead and create a statement. And we're just going to use a simple text statement, and I'm going to type in "debug.log". And you're going to notice that we have all of these different options here as we're typing, and this is called IntelliSense. And this is using our API, which it stands for automated programming interface. All of these are drawn from our namespaces that we're using here. So we have debug.log, and then we're going to open parentheses, and then we're going to do open quotation, so the double quotations there. And then we're going to just type in a sentence here. So I'm going to type in "this is a statement." And then I'm going to end this statement with a semicolon, so this is kind of like the period or the ending punctuation of a sentence. This will always be at the end of a statement. All right, so now let's go ahead and hit Save, and then we're going to go into Unity, and then I want to place this script on to a game object. So I'm going to go to Game Object, Create Empty. Let's go ahead and drag this lesson_02 script on to that Game Object, and we can select that Game Object, and you'll see that that is now tied to it as a component. If we hit play, what you'll see, right down here on our console, is it says this is a statement. Now I want to bring out my actual console, so I'm going to go to Window and then Show Console. Now you'll see that "this is a statement" is being created from UnityEngine.debug.log.object. So again, it's using that namespace. It's drawing from that, and it's looking at its debug so it knows what to do here, and it's going to place our log, which is what we created, our text, OK, inside of our console. So notice that whenever we've put that statement in our start function, it's only running it one time. Let's go ahead and go into our script again and let's create that same line of code right here. Let's just select it, right-click, and Copy. And let's put that in our update function and see what happens. So this is a statement. Let's go ahead and let's say this is another statement. Let's go ahead and save this. And then we're going to go into Unity, and now, we'll see that that is now running. Let's go ahead and stop that. Let's clear. Let's hit play again, and you'll see that it keeps running. "This is another statement." And it's going on and on and on as long as we keep the game going. Let's turn that off. OK. So here, you can see the difference between these two functions and how they work. All right, so now that we have these statements here, let's talk about another term that you're going to hear. And I'm going to be saying this quite often. I'm going to be saying let's take a look at this block of code. And what I mean by that is a series of code that is grouped together. So you may see that as a function, or you may see that as a series of statements. So I could come in here and I could say debug.log, and I'm going to say this is a block of code. OK, and I could do another debug in here if I wanted to. They continue on. And this block of code is longer. K, and notice how I'm ending all of these statements with that semicolon. this is going to be extremely important here. All right, so this is essentially a block of code here these are all grouped together, and they're all meant to stay together. OK, this is also a block of code. This is a function, and so it's going to represent a single block. Now that we have talked about statements, and we know what functions are right now, at their very core, let's go ahead and talk about comments and how they're going to help us throughout our process. So we can see two examples of comments here. We have this first one that says use this for initialization, and then it says update is called once per frame. OK, so comments are going to be useful for making notes, giving directions, and helping us during the debug stage. So let's say that I wanted to create some simple notes. So to start out, I'm just going to come right in here, and I'm going to use what's called a single line comment. And that can be done by doing a double forward slash. OK, so that is the key left the right Shift key. OK, so double forward slash, and then you can type anything that you want in this line. So this is a comment or a single line comment. OK, and then if we want to create another line for our comments here, we could just do another double forward slash. This is another single line comment. Now after a while, we are going to run into situations where we have blocks of code that we may want to comment out. So let's say that we are finished with all of these debug logs, and we no longer need those to write. So what I can do is I can come in here, and I can hit a single forward slash, and then I'm going to hold Shift, and then 8, and that's going to give me an asterisk. Now you'll notice that it is commenting out everything below it. This is called a multiline comment, OK. So this is a multiline comment. Now, you'll notice that everything below it is going to be commented out. So we need to be able to close that multiline comment. So to do that, let's go after what we want commented out and then we're going to type in Shift-8 and then Forward Slash. And now you'll notice that we still have those curly brackets, and they are no longer in that green color. So everything that is commented out is between this asterisk and forward slash and this one. All right. Now, we also have the ability to use commenting to help us with debugging. So let's say that we type something in here, it's just not working. It's giving us all kinds of problems. So let me come in here and let me say something like player equals sugar plus spice. OK? This is a statement here, and I may think that this is totally fine. Let me go ahead and save this. And let's going to Unity and you'll see that we have these errors that are showing up, and it's saying the name sugar does not exist in the current object. So we're getting an error, and we can no longer run our scripts, and our game will not run properly. So a way to get around that to start debugging and start figuring out what the issue is, is we can come in and just simply comment that out. Re-save that and go back and you'll see that that error is now gone. So now whenever we hit play, you'll see that our console is running the way that it should. All right, so now that we've talked about what classes are, functions, statements, and comments and how to use them, let's start taking a look at variables and how they can help us improve 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