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.
Creating the player spawn controller
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we will create our player spawn points and create a script that will spawn the player randomly on any one of those spawn points. All right, so the way I want to set this game up is I want to create spawn points randomly throughout this level. And any time that the player plays, they will randomly spawn at one of those points. Now, this may take a couple of lessons to get through. But I'm going to show you how we can set this up. First thing that I'm going to do is I'm going to go ahead and get rid of my message machine. So I'm going to just delete that from my hierarchy. And we'll still have our hello world script. So you can take a look at that if you want. And let's go ahead and create a new empty game object. And let's go ahead and call this Player Spawn. And let's go ahead and call this _01, because I'm going to be numbering these as we spread these out. So now that I have this player spawn, let's go ahead and get it positioned in our scene to where we know we can get it. And I'm going to go ahead and place this right here in the beginning of this level. I'm going to Control-D to duplicate that. And we're going to go ahead and rename that one. So I'm going to call it playerspawn_02. With it selected, let's move it to a different point in the level. So I'm going to snap this over here. You could hold Control to move that just to make sure that it snapped. It's not absolutely necessary. There we have that. And then, we're going to go ahead and make another copy of that. So Control-D. Let's rename it. Here we go. And let's move this into a different position. So we're just going to move this over to this portion right here. Now with that right there, let's hit F on the keyboard to frame to that object just to get there a little bit quicker. All right, so now that I have that one in place, let's do one more. So I'm just going to move out here a little bit. I'm going to hit Control-D. And let's say playerspawn_04. Select that. And let's move that into position. I'm going to hit F to zoom in on that. And there we go. And I think that should be all the ones that we need. All right, so now that I have that, lets go ahead and start talking about how we can spawn at these positions. Well, the first thing that I need to do is I need to group these object into a tag. Now, a tag can be found with one of these objects selected right here in your Inspector. So you'll see the player spawn name. And then we also have a tag. Now, right now, this is untagged. If I click on this, you'll see that we get a list. And I have several different lists here. I have tags. I have re-spawn, finish, main camera, player. Now, I could use re-spawn. But I'm going to go ahead and just add a tag here. Now, with this tag, I can create it right here under our tags. And you'll see that I have a size of one. And in Element Zero, let's just left-click in there. And it should give us this text. And we can type in whatever we want here. So I'm going to go ahead and call this playerspawn. Now with that, if I come out here and I go to player_01, let's go ahead and go to our tags. And you'll see playerspawn has shown up. And now I can do this to all of my player spawns. It doesn't have to just be that one. This is going to allow us to access these objects through code using these tags. All right, so now that I have those set up, let's come in here and let's select our first person controller. And let's make sure that it is tagged as player. There we go. All right, so now that we have these, we are ready to get into our code. So let's go into MonoDevelop. And we have our hello world. But first thing that we need to do, obviously, is to create our script. So back in Unity, we're going to right-click, Create, C# script. Let's call this playerspawn controller. And let's double-click on this . And it will come up in MonoDevelop, as you can see here. And we can begin to add our scripts that we want to. So whenever you're scripting, it's always best to go ahead and just talk it out and begin to take note of the words that you say. So things like OK, what I want this to do is whenever the game starts, I want my player to randomly spawn at one of the designated spawn points in the level. So a couple of key words that I heard there was at the start of the level-- so we know that we need to have something starting in our start function, or even our awake function-- we'll talk about here in just a moment-- and I need to get some sort of random number that's going to grab from my list of my spawn points. So I know that I'm going to be needing to use an array. Now, right now, I've used some words that may be a little foreign to you, so things like array. Arrays are essentially lists. So what we have is a list of player spawns, or we need to create a list of player spawns. And then we're going to get a random number from those. All right, so let's get started with this. And I'm just going to step you through this. And again, if any of this is foreign, that's OK, because you're just going to be getting an exposure to this. And then if scripting is something that really interests you and something that you really want to get into, go ahead and check out those introductory courses on scripting in JavaScript and C#. So starting out, what I'm going to do is I'm going to come right up here to the top. And I have my player spawn controller. And I'm going to go ahead and bring that curly bracket down here just by hitting Enter. And I like to do this for my start and my update, as well. The reason I like to do this is because it helps me keep in order my curly brackets. And these are going to be extremely important in scripting. So let's go into above our start. And I'm going to type in a new function that is used by Unity. And we're going to say void, awake. And then we're going to open closed parentheses. I'm going to hit Enter one time and then hold down Shift and curly bracket. So we're going to do that. And inside of those curly brackets is where my scripts are going to live. So the first thing that I need to do in my awake function is I need to gather a list, or gather all of my objects, my player spawn objects. So let's type in playerSpawnArray. And we're going to set this equal to GameObject.FindGameObjectsWithTag, because we have multiples. And we already set these tags up. So the first one that we're going to be doing here is player spawn. Now, if I've lost you already, that's totally fine, because scripting can be a little complex. It's like learning a brand new language. So don't get intimidated by this. I'm going to try to explain these as best as I can to you. So what we've just written right here is whenever the game wakes up, at the very first frame, it's going to go ahead and find all of these game objects. So it's going to go-- and we're creating a variable here-- to hold this data. And the data that it's going to hold is our game objects that have the tag player spawn. So we've already created those. And so player spawn is going to hold that data for us. Now, we've created a variable here. But we have not initialized this variable. So what we need to do is create some variable initialization. So inside of our public class, I like to put my variables right up here at the top. And I like to put them right all the way over to the left. It just seems to help. So the first thing that I'm going to do is I'm going to type in public. Now public is going to make this variable public to any script across our game. So we're also going to type in, after public, GameObject. And one thing that I want you to notice is GameObject is in blue. And it's also coming up through this what's called Intellisense. And Intellisense is just a great helper whenever scripting. Now, what am I writing here? What is GameObject? This is the type for our variable. So the variable that we're putting in here is the type of game object. So it's going to be looking for a game object that's in our scene. Now, we wanted a list. We wanted an array for this. So with arrays, they must have the open and closed brackets after those. Unity will recognize that we are looking for a list, or an array, of game objects. So it's looking for multiple. So we're to type in playerSpawnArray. And after that, we're just going to simply hit semicolon. So once that has finished, we can come in here and we can create another variable. And I need one more variable because we're going to be getting a random number. Now, I'm going to go ahead and wait on this variable. We're going to go ahead and stop the lesson here. And in our next lesson, what we're going to do is we're going to go ahead and we're going to talk through the process of getting a random number, and grabbing one of those random player spawns, and making that the main spawn at the very beginning of our game. So I'll see you then.
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.