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.
Loading levels
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this tutorial, we're going to take a look at how to create the Win State and how to load a new level. All right, so up to this point we've gone through several lessons on how to work inside of Unity from an art standpoint, where we've gone in and we've created the level, we've added props, we've learned how to create pre fabs and manipulate objects and all kinds of different things. And then we've got into an actual scripting and creating games, and game rules and systems, and now we're ready to start wrapping up this course where we're going to discuss creating a Win State and then loading a new level. So creating a Win State is basically checking to see if we have won the game. And this could be as simple as colliding with an object, moving into a trigger at the end of a level, defeating a specific enemy and their health becoming zero, or even, you could create a Lose State to where the player's health reaches zero, and then it has to restart the level and the player has to spawn. Really, you could get as complicated as you want with any of these Win States. Now, what I brought in into the level is a shrine object. And it's basically just kind of a little monkey statue with the material on it. And this object has a box collider on it, and the box collider is a trigger. And so whatever the player comes in contact with that, we're going to load a new level. So I'm going to put this in near one of the spawn points here, and what we're going to do is, we're just going to collide with that, and then we're going to have our script load our level for us. Now, whenever I say load level, I don't necessarily mean that it's going to load a brand new level like this one. It could load something like your high scores menu, or your main menu. It could load the credits, if you wanted to. All of those-- high scores, main menus, and credits, are actually just new scenes. So let's go ahead and get started with this. So we have our shrine in our scene here. And what I want to do is go ahead and create a new script. So inside of my scripts folder, let's go ahead and create a new C Sharp script, and we're going call this, Shrine. Double click on that Shrine script and let's get started with this. So we want to make this one pretty simple, because I've thrown a lot at you with scripting and again, if you are really interested in scripting and you want to know more about it, we have two great tutorials to follow along with. We have Introduction to Scripting in Unity, which is going to take you through the JavaScript scripting language. Or, you can go through the C Sharp scripting language with introduction to C Sharp in Unity. All right, so like I always do, I always like to set up my brackets just so it helps me with pairing. But we're going to go ahead and go to the end of this, and we're going to say, void on trigger. OK. On trigger, Enter, excuse me. We're going to say collide, or collider player. So just like we'd done before. And we're going to say, player.gameobject. Let's try that one more time, .gameobject.sendmessage. Actually, we don't want to send a message this time. We want to actually tell it to load a level. So we're going to say .gameobject.application.load, level. And what level do we want to load, OK? So in this case, I'm going to say Main Menu. So now that we have that, that's really all we need to do. Now, whenever we are using this application, .loadlevel, we need to understand that what this is going to do is, it's going to load the new level and destroy everything in this scene. So it's going to remove everything from memory, it's going to destroy your first-person controller, and things like that. So whenever you're loading the main menu, that's probably the best thing to do. So remember, if you need something to carry over, you may want to use a different method. So let's go ahead and save this. And let's go into Unity. And it's giving us an error saying the application is not of the type game object, OK. I see what I did here. I added player.gameobject, and really all we need here is application.loadlevel. So let's go ahead and save that. Go into Unity, that error has disappeared. Let's play. And we're going to go in here and we're going to find our shrine. And I believe it's right down here. And if we don't want to keep searching, what can do is, we can go to scene really quickly, figure out where we're at, where the shrine is at. And then that might make things go a little bit easier. We're all the way on the other side of the level. Let's go ahead and just quit that, and I'm going to hit Play. And let's go until we're at Player Spawn 01. Hopefully this doesn't take too long. There we go. So here we have our lantern. And then if we collide into our object, you'll notice that nothing is happening here. So we need to have something that is going to recognize our player. So we need to check and make sure that our object is even colliding. Now, I already know what's happening here. We created our script, but we didn't actually attach that to our shrine. So let's go ahead and attach that. There it is. And let's say , use light probes for our object here. And we're going to hit Play. And we're already here, so that's good. And then we go ahead and we hit our shrine, you'll see here that it's saying, Level Main Menu couldn't be loaded because it has not been added to the Build Settings. So we have our script, we've created a very simple Win State. And it could just be collecting objects, we could add health to the shrine, and let's say that we had a weapon and we were striking it and we had a variable that gave it damage, and it was keeping up with the object's health, and we had to destroy it, or something like that, and then it would go into the Load Level. I'm just showing you that the Win State can be very easy, and loading a new level is very simple as well. So now that we have that, this brings us into our next point where we're going to have to learn how to build our games, and how we're going to set these up to have multiple levels. All right, so we're going to go ahead we're get started with that, next.
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.