What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson, we're going to learn how to create a new script asset, how to print out information into our console, and begin by creating a custom variable. So here's where we left off. And you can open up to lesson 26 to jump to this point. So now, let's begin our custom scripting by creating a new script asset. So let's go over to Assets, Create, and here we run into our first difficult question. Do we want to script in JavaScript, C#, or Boo, which is a variant of Python? So this is, of course, dependant on what you have experience with. If you have experience with any of these three languages, I would pick the one that you have experience with. Otherwise, we're going to start with JavaScript because it's the simplest to write. So let's just create a new JavaScript file. And let's call this our character controller, or instead of that, let's call it our character state. We can, of course, call these anything we like. And I'm just going to create a new folder for my scripts, just again for cleanliness. And let's copy in-- or, let's move in our character state. So we'll move that over. And now, we can double-click this to begin editing our code. And that's going to launch Mono Develop, which is the built-in code editor that comes with Unity. So here, we have Mono opening up our characterstate.javascript file. And here's where we will add in all the code. So let's begin with a classic example, the print "hello world." So let's just give ourselves a little bit of space in the update function by hitting Enter a few times. And let's hit Tab to jump over. And let's just type print, and then open parenthesis, which is above the nine. And let's just put a quotation mark. And let's just do our usual hello world. And we'll, again, close this with another quotation, which says to our scripting language that's a string. And let's close it with an end parentheses. And finally, every line needs to end with a semicolon. So this is what's going on here. Update is a function that is called every single frame on our objects. A function is simply a container that holds various commands. So whenever the game asks our script, hey, do you have anything for this update? We're going to say, yeah, you should print out "Hello, world" into the console. And so if we hit Save or Control S and we jump back to Unity, and let's open up our window console and hit Play. We will actually not be able to see anything. And that's because, like every other asset we've ever created, nothing happens until we bring it into our hierarchy. So scripts can live on any game object. Since this is a character script and it's going to be working with our first person controller, let's actually just drag this onto this first person controller. Now, it's going to ask us if we want to lose the pre-fab connection. Normally, we do not want this. But since this is simply one of the standard assets, let's just click Continue. And now, our character state is going to appear last in our inspector. So now, if we hit Play, we should be able to see in our console that "Hello, world" is being printed every single frame. OK, great. So now that we have that down, let's begin talking about declaring variables. So let's go back to our Mono Develop. And instead of-- let's just leave this here for a while. Now, when the script begins or when the script is initialized, we have the chance to set information or data. And really, all scripting is or all coding is, is taking in some information or some data, changing it, and then sending it along to the next script. Now, for our character class or our character script, I would really like to track the health. Now, health is, of course, just a random name we've given to us some idea of life inside of a video game. Now, to actually define this in our game world, we need to give it a name and plug it into a variable. So to create a new variable, we can type V-A-R for new variable, and then we need to give it a name. So let's just call it health. And let's give it a-- and semicolon so that it will run. And now, we have this variable, health, which we can store information in. So currently, if we save this and hit Play, nothing really is going to change. Our console is still going to write out "Hello, world," and our health is going to exist in memory space. However, we don't know really what it is yet. So let's say, variable health equals 100. And now, we're assigning a value to this variable. So now, health is going to start out with 100. So now, instead of printing "Hello, world," let's actually print health. So I'm just going to copy this, paste this inside the print here. And let's actually do something that is going to break initially. But if we keep this inside of quotes, you'll notice it turns pink. And that is because Mono is interpreting this as a string or as a value. So when we say print health, if it's inside of quotation marks, instead of actually looking at the value, it's just going to print the word "health." So if we go back to our console, give Unity a second to update and hit Play, we should see it says "health" instead of giving us the value. So whenever we're using a variable, we need to make sure it's not pink and it is, instead, just this regular text color. So now, let's hit Control S to save that, come back here, and hit Play again. And now, we will see 100 being printed out inside of our console. OK, this is all well and great. Now, instead of printing "Hello, world," were printing 100. But now, we can actually update the health during the update function or any other function, and we can now print out the current health. So if we do something like subtract 1 from the health every frame, we can see that being updated. So let's type in health minus equals-- which is saying health equals health minus-- 1 semicolon. So every single frame, health is going to be reduced by 1, and then Unity will print out our health. So let's hit Control S and play this back. So now, we should see our health decrease as the frame rate goes on. Now, of course, Unity doesn't know what health is. So it's not going to do any killing. It's not going to change anything. It's just tracking some random number called health. And we, as programmers, are the ones who have to give it significance by using this health inside of our scripts to change the behavior. OK, so we have our script working, and printing out, and changing our health in the console. But I'd really like for the player to be able to see it in the game window while they're playing, or at least be able to somehow represent the health in our game world so that our player knows when he's getting hurt or when he's about to die. So in the next lesson, we'll take a look at a very simple OnGUI function, and then use the GUI box to actually draw our health on the actual game screen. So in this lesson, we created a new script asset, took a look at attaching scripts to game objects, and began learning how to use JavaScript to store information in variables and then print out that information during the update function.