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 script a level ending, or loading a different level, as well as how to use comments in our code to document our processes. So here's where we left off. And I've actually just created a new scene here called Main Scene. And I've also created an ending, but we'll get into that in a second. Also, between lessons, I've gone in and just added in a third door trigger to open up the doors at the end of this level. So once this level ends, we want the application to load the next level, or in our case, another scene. So to do this, we can do it many different ways. But I'm going do it with a trigger. So let's just select one of our triggers, let's say our door three trigger. And hit Control D. And let's just move this into the final doorway here. And let's make it a little bit larger and move it forward a little bit. So once the player hits this trigger, we want to load a new level. So let's call this our end trigger. And again, this name doesn't have anything to do with anything except it lets us keep track of things. Now, we can't use the activate trigger script for this. So let's right click and remove these two components. And we're actually going to need a new script asset. So let's go to Assets Create. And let's just create a new JavaScript. And let's call this Load Level. Just hit Enter on that. And this can be a JavaScript file that we can use to load any level. So let's drag this now onto our end trigger. And now you can see we really don't have anything yet, because of course we need to add in the data in this script. So let's double click this to open it up in Mono. And since we're using a trigger, we can reuse some of our code from the character state. So function on trigger stay, the collider. Let's go to our load level here. And let's just paste this in, because again, this is what will trigger, or what will be called once something triggers this collider. Now of course, if we have objects moving around a scene or multiple characters or entities, we may want to check to make sure that it's the player hitting this end level and not some random piece of physics. However, since our current game's pretty simple, we don't really need that. So now let's take a look at how we can load a level, or load a different scene here in Unity. So as usual, I'm going to jump over to the scripting reference. And let's take a look at level. And remember, levels isn't what they're called. They're called scenes. So let's take a look at scene. And yet again, I don't see anything. So the actual code is called load level. And this is what Unity uses inside to load a level, or a scene, based on its name or index. So let's click this. And we can see here application.loadlevel, and then just give it the name of the scene that we want to load. So this is very simple. Let's just Control C copy this into our Unity or our Mono development. And let's paste this in. Now of course, we don't-- or if we have a level or a scene that was called High Score, we would leave it as is. But in our case, we want to load the ending level. So let's just type in ending here and hit Save. And let's go back into Unity and take a look. So let's hit Play. And let's run to the end of this game. And I'm just going to hit Control Shift P to pause it, and I'm going to accelerate my motor again. I have very little patience when game testing, so I usually set my character speed up quite a bit just so we can get to the end faster. So we have our door trigger, and now we have our load level. But you'll notice here at the bottom it says, level ending couldn't be loaded because it has not been added to the build settings. And that's important, because we need to add this scene into our build if we want to be able to load it during our game. Now, Josh is going to cover that in the next lesson. So for now, let's just take a quick look at how we can clean this script up a little bit and comment it. So currently, we have the level ending hard coded. And if we want to use this on other objects, it would be great if we had a variable that we could change this ending, or change what level we're plugging into this. So let's just create a new variable of the level we want to load. And put a colon, and let's call this a string. Now it would be great if we could put a scene in here, but instead, we'll just need to use the string to give it a name. So let's put a semi-colon there. And then, instead of load level ending, let's put load level, and then put the level there. So now, by doing this-- and let's hit Save-- we can now jump back to Unity. And inside of our inspector, we can now come in and define what level it's going to load. Now it's not appearing inside of my inspector, and that's because we made a slight mistake when we wrote this. You see here we used a lowercase s, and this did not turn green. And that should have been our first notice that it was a problem, because types are always in a green color in Mono. So if we change this to a capital S and hit Control S to save it, we should now be able to see this inside of our Unity inspector. And now, if we take a look, we now can type in the name of the level. So if we put in ending, we will now pass this value into this script and then execute this code so that we can type in any level. And now, we can use this as a trigger script for any object. So if we have multiple exit points, all we need to do is copy this to our other triggers and then type in whatever level we want to load. So if we wanted to load Lesson 31, we can do it here as well. And by building these changeable scripts and these template scripts, we can really save time while we're developing inside of Unity. I'm just going to remove that because we don't really want to load Lesson 31 when we hit door three. So finally, commenting. And commenting is incredibly important. So you can comment, or you can add a comment to your code by hitting backslash backslash, and then anything you type here will not be executed, or not be run. So this is a great way, or a great place, to come in and comment your code and leave notes for yourself, and leave notes for the next person. So usually, most classes, or most script files I create, I give the name of the script. So load level.js. Then I'd say what I'm trying to do with this script. So this is used to load a level set in the inspector. So we'll hit Save. And adding these comments is not going to add anything to the runtime. It's not going to change anything inside of Unity. These are just notes for us to keep. And these are really useful once we get to much more complex scripts. So this is the player's health. This is how fast the player loses health. And then, this is a container for the starting health. And then we can come in and comment what these functions do. For example, reset might be very clear. So we might add a comment, this resets the health and position, and anything else we do. And so let's see here. Damage if end fire. So by adding these comments into your own scripts, and by keeping well-commented code, you're going to make it much easier in the future for you to come back and remember, oh, OK, I can run the function reset to reset my health and position. Or for example, if he's following through the ground, I might want to do something else then reset it. Or I might want to add a command to print something to the console that the player is falling, or some other solution here. And I'll only remember these things by keeping well-commented code. So now that we've wrapped up our scripting portion of this course, I'm going to hand it back to Josh to finish this off by going over the build settings and how we can finally publish this game into an executable that we can then sell or give to other people. Just going to do a little bit of cleanup, move my scripts in, load down my triggers. And then we'll be ready to build our game and play our completed game.