Important Notice:

The Digital-Tutors site will soon be retired and your access will be turned off - but don’t worry, you can still enjoy all your favorite Digital-Tutors content on Pluralsight (plus thousands of additional courses, skill assessments, paths and more). You can log in to Pluralsight with your Digital-Tutors credentials, as well as update your password at any time on Pluralsight. Please contact us with questions or concerns: support@pluralsight.com.
Finishing the player spawn controller
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to finalize the main functionality of the player spawn controller script. All right. So open up the player spawn controller script. In the last lesson, we had created a variable called playerSpawnArray. And an array is basically just a list. Now, with our playerSpawnArray, we've given it its variable name, but the kind of variables or kind of data that it's going to hold is going to be a GameObject. And so the game objects that we want to put into this variable or this array are going to be our player spawn game objects that we had created. So now in our awake function, we are setting that to any game object that has the tag PlayerSpawn. And so all of those player spawns that we created have that tag. And so whenever it finds one, it's going to add that to the first place in our array, and that's going to be an index of 0. If it finds another one, it's going to add it to the second place in the index, and that's going to be the index of 1. And so it's going to kind of look something like this. You don't have to type this out. But just to help you visualize this, it's going to be GameObject, and then whatever the index number is. OK. So in the first place, it's going to be an index of 0, and we're going to set that to playerSpawn01. OK? Now, that's just kind of what it looks like in code. We'll see this inside of the Inspector here in just a moment. So let's go ahead and, oops, let's not add that there. Let's go ahead and get rid of that script there. All right. So now that we have established what we need to do, we want to get that index, and we want to randomize that index. So the array is going to build out its list, and it's going to have all of our player spawns in it. And now what we want to do is we want to randomize which index in that array is going to be used for our player spawn. And so in order to do that, we'll create a random number. So to get started here, let's go ahead and create a new function, and I'm going to call this void. And we're going to say, GetRandomPlayer-- oops, RandomPlayerSpawn. Open/closed parentheses, and then we'll open/close curly brackets, and we'll come back up here in the middle. Now, we need to create a variable that is going to hold that random number, that's going to get that random number. So I'm going to say randSpawnID, and we're going to set that equal to a Random.Range. And then inside of that, we're going to put a parentheses here, and we're going to tell it what is the range. So we need a minimum number, and we need a maximum number. So the minimum number that I want is going to be 0 because our array will always start with the number 0. That's the first place. Now, we want a max number in that, and we're going to say playerSpawnArray.length. OK? And let's go ahead and put that in there. And let's go ahead and put this in parentheses here so that way it recognizes that there, and then we'll end with a semicolon. So what we're saying here is we're going to get a random number between 0 and then whatever the length of our spawn array is going to be. So once that has been set, we need to do something with this. So let's go ahead and just make sure that this is even working. So we're going to do a simple print or debug.log, whichever you prefer. Either way is fine. And we'll say SpawnID, and then we're going to add the variable randSpawnID. Now, you'll notice that it's not coming up, and that's because we haven't initialized that variable here. So let's go ahead and just make it a local variable to our class. And we're just going to come up here, and we're going to say public-- actually, let's make it a private randSpawnID. And we'll just leave it just like this. And let's make sure that set that to an int because that's what we're going to be getting is an integer value, and let's hit Control-S. So to save that, let's go back into Unity, and let's make sure that our player spawn is going to be on a game object. And so what we'll do is we'll create a new empty game object. And I'm going to go ahead and just zero out the values here, so type in 0, 0, 0 all the way across, and rename this to Controllers. And let's make sure that we spell that correctly, Controllers. And then we're also going to create another game object, another empty game object, and we'll zero that one out as well. And I'm going rename this to Player Spawn Controller. All right? And so with that set, let's go ahead and parent that to our controllers, just for organizational purposes. And then with the Player Spawn Controller selected, let's go ahead and just drag and drop our player spawn controller script onto that. Now, here you can see the Player Spawn Array, and as of right now it's set to a size of 0. And the reason for that is because whenever we play on the awake function, it's going to find all of the player spawns with the tag of Player Spawn, and it's going to add those to a list. And so let's watch this in action. Let's hit Play, and there you'll see that it's changed our size to 4. And now we have player spawn 3, 2, 4 and 1 all set to the elements of 0, 1, 2, and 3. The element 0 is a representation of index 0, index of 1, index of 2, and an index of 3. OK? So hopefully that makes sense, and you can visualize that. now. All right. So now that we have the player spawn controller script tied to that, lets hit Play again because I really wanted to see our message being printed out. And you'll see here that nothing is being printed. And let's go ahead and go back to our script, and let's make sure that that is working. Now, it's not really working because we haven't called this function. And so, just for right now, let's go to our Start function, and let's call that function. So we're going to say GetRandomPlayerSpawn, open/closed parentheses, and then end with a semicolon. Hit control-S to save that, and then we'll go back into Unity. We'll hit Play now, and you'll see that we still get our array in the Inspector, and now we're looking at the Spawn ID. So which one is being randomized? Well, we're now on Spawn ID of 1, and so that would actually spawn us at Spawn 02 because it's an index of 1. That's what we're looking for. OK. So we could go ahead and we can hit Play. We should be able to get a different Spawn ID. And we can see that it is randomizing every single time that we press Play, every time that we play here. So now what do we want to do with this? Because what we've done is we've created a random number generator. We've printed that out. But we're not really doing anything with that information. And so what we're going to do in our next lesson is we're going to create the game state script, which is going to call this function, and it's just going to handle the spawning of the player and putting the player in its proper place. And so we'll get into that in our 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.
Introduction and Project Overview
1

Introduction and Project Overview

 
01:14
Breaking down the interface
2

Breaking down the interface

 
10:59
File management and project organization
3

File management and project organization

 
10:45
Creating prefabs
4

Creating prefabs

 
10:41
Creating materials
5

Creating materials

 
10:00
Assembling the level
6

Assembling the level

 
08:36
Set dressing the level
7

Set dressing the level

 
14:09
Adding lights
8

Adding lights

 
09:55
Baking lightmaps
9

Baking lightmaps

 
11:32
Creating particle systems
10

Creating particle systems

 
11:19
Working with physics
11

Working with physics

 
09:41
Getting started with scripting
12

Getting started with scripting

 
09:41
Creating the player spawn controller
13

Creating the player spawn controller

 
11:10
Finishing the player spawn controller
14

Finishing the player spawn controller

 
08:12
Creating the game state script
15

Creating the game state script

 
11:46
Scripting the HUD
16

Scripting the HUD

 
13:16
Creating items in Unity
17

Creating items in Unity

 
13:35
Creating the lantern functionality
18

Creating the lantern functionality

 
18:32
Loading levels
19

Loading levels

 
08:08
Publishing our game
20

Publishing our game

 
08:46