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.
Getting started with scripting
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we're going to begin exploring how to use scripts to create rules for our game. All right, so up to this point, we've been bringing in content for our game. And we've created a little bit of interactivity by using a simple first-person controller that comes with Unity. But we really haven't created a game yet. Now, a game is essentially a set of rules and systems set in place for the player to follow. And this is going to be creating game play. So to create these rules and systems, we need to create what are called scripts. So the first thing that I like to do whenever creating scripts is to create a scripts folder. So we're going to right-click in our Assets folder and then create folder. Let's call this Scripts. And this is for cleanliness, keeping things organized. And we're going to go into our Scripts folder. And we're going to right-click. And we're going to create a new script object. Now, you can use three different languages that are inside of Unity. You can use JavaScript, C#, or Boo. Now, whichever one you are most familiar with, that's the one you should probably use. Now, for this course, we're going to use C#. So whenever you create a new C# object, you'll see that you have the ability to go ahead and name this C# object. It's important that you name this properly, that you have some sort of naming convention down for this. So for this first one, we're just going to call this, hello world. Now, the hello world script has probably been used to death in Beginning with Scripting. But I think it is the most helpful in this case. So whenever we create a C# script, let's go ahead and double-click on that. And what this is going to do is launch what's called MonoDevelop Unity. And this is going to be the scripting environment that you will primarily use for Unity. Now, if you have something like Visual Studio, or something like that, you could use that. There's nothing wrong with that. But MonoDevelop comes with Unity. And I would really recommend using it. It's a great editor. So this is what we get in a C# object here. You'll see that we have a little bit of code that has already been written for us. And this is a great way to get started and to begin understanding how scripts work. Now, we're not going to be discussing exactly how scripts work and all of the vocabulary that goes along with that, because we have a course that's dedicated to that. We have a couple of different courses. So if you want to learn introduction to JavaScript, you can go to the Introduction to Scripting in Unity. That's going to be the JavaScript version. And then you can go to Introduction to C# in Unity to learn about C# scripting. And they're very, very similar. All right, so to get started here, let's take a look at this script and just analyze what's in here. The first thing that we have with a C# script is you'll see that we are using these different collections. So what this is going to do is it's going to give us access to a lot of precompiled code that is being used by the Unity engine. So by using this, we have a very powerful piece of equipment here. What you can do is you can go to Help. And then you can go to the Unity API reference. And by using this collection here, everything that's inside of this reference is going to make perfect sense to the editor itself. Now, we also want to use the system collection. So that's going to be your operating system. And we're going to be able to understand-- or the script is going to be able to understand specific scripts and functions that are going to used. Now, the first thing that you'll see is we have a public class. Now, what is a class? Essentially, with C#, a class is a script. So in this case, you see that we're using helloworld.cs for C#. And the class is actually called helloworld. Now the type, which you can see here, is a Monobehavior. Now, a Monobehavior inside of Unity is essentially a script object. Now, you'll see that we have two functions. And they are predetermined by void. And what void means is it's a function that doesn't return any information. So if you want to learn more about returning and returning information, you could always take a look at those Introduction to Scripting courses. Now, you'll see that I have two functions that are in here first. I have Start. And I have Update. We're going to go ahead and talk about what Start and Update do, because they are going to be crucial in scripting. So let's go ahead and get started here. We're just going to use a very simple script to print out a message to our console inside of Unity. So let's go ahead and go to our Update function. And inside of our Update function, we're just going to say debug.log. And then inside of parentheses-- so I like to do open close parentheses-- and then go back inside of those parentheses and tell it what kind of message to print out. So your message for this log should always be in quotations. So you're going to hold Shift and then use double quotation there. Now inside of that, you're going to produce the string, or the message, that you want to print out. So we're just going to say, hello world. So this should print out to our console. Now, with every statement like this, it should always end with a semicolon. So inside of MonoDevelop, if we go ahead and save this and we go back to Unity, we can go ahead and play our game. And then, if we go to our console, we should see a message. Now, let's wait for this to play here. And let's actually turn off the Maximize on Play for our game here. I'm going to hit Play. And my game has started. And if I go to my console, you'll notice that there's no message being printed here. Now, I did this on purpose, because it's key to scripting inside of Unity. All scripts must be in the hierarchy. And they must belong to some object in the scene. So in this case, we have our hello world. But we didn't attach it to anything that is actually in the scene. So what I'm going to do is I'm going to go ahead and create an empty game object. And I'm going to call this Message Machine. And then we're going to go ahead and drag and drop this script right on Message Machine. So I can left-click and drag into my hierarchy. And I can drop that onto the game object that I desire. Now, by doing so, you could see that it shows up as a component of that object in the Inspector. Now, if I hit Play here and I go to Console, you can actually see a little bit of a preview of what's happening right here along the bottom. Now, if I go to the console, look at what's happening. Notice how many times that that message is being printed right now. It is actually printing this message every single frame of the game. So why is it doing that? Let's go back to MonoDevelop. The reason that it's doing that is because Update is going to be called once per frame, as you can see that right here in your comments for your scripts. So what happens if I were to take this script-- let's go ahead and select it-- and I'm going to right-click and cut that-- and let's put it in our start. So if I right-click and paste that, you can see in your comment, it says this is used for initialization. So in your mind, and in my mind, I would think, OK, this just going to call this at the very start of my game. But is it going to continue to call that like the update? Let's go ahead and save it and find out. So if we go into Unity-- let's go ahead and, in our console, let's hit Clear. Now, that will always clear on play, anyway. But I like to go ahead and just clear it out. And let's hit Play. And you'll see that that has only been called once, as you can see it right here and here. All right, so we know that our start function will only do a specific action once in that function. And that's going to be done at the very start of the level, whereas update is going to do it every single frame. All right, so where does update come in handy here? Well, this is a place where we would run things like checking the game state, checking for whether or not the game has been won or if the game has been lost. That could be whether the [? boss ?] has lost all of its health or the player has lost all of its health. So now that we've got an idea of how to create scripts and a very basic look at the anatomy of a script, let's jump into actual scripting by creating our random player spawn. And you're going to see lots of different pieces to a script. And we'll jump into those. And we'll discuss those as we go along. All right, so I'll see you in the next lesson.
In this series of Unity tutorials we are going to learn about the core features in Unity.

We will start out by learning the Unity Interface where we'll talk about the different panels and tools available in the Unity editor. From there we'll learn how to properly export and import assets into Unity. With those assets imported, we'll discuss how to create prefabs that will help us speed up the construction process of our level.

Then we'll learn how to create and apply materials to our level prefabs. Then we'll take our textured prefabs and build a simple game environment. Once the base level has been built, we will talk about adding props and set dressing our level. From there we'll continue full speed by learning how to add lights, particles, and physics objects.

Once we're happy with our level, we'll jump into scripting in Unity. We'll learn how to create a random player spawn, a HUD, item pickups, and so much more. Finally, we'll end the course with how to publish our game to the platform of our choosing.

For an additional learning resource, download your free copy of our Key Game Development Terms Reference Guide and PDF so you can get comfortable with important game dev terminology.