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 game state script
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to create the game state script. All right. So inside of Unity, let's go ahead and create a brand-new script inside of our Scripts folder, and we're going to call this Game State. Now, while we're here, let's go ahead and create our game object that this game state script is going to live on, and so we'll go to our player spawn controller script. I'm going to hit Control-D to duplicate that, and we'll just rename it to Game State. Now, also with our game state, or our player spawn controller script, we're going to need a tag for this. And so I want to go ahead and go to Tag, and then add my player spawn control script, or tag. Now, if you haven't done so, go ahead and just Add Tag, and then type it into one of these elements that you have open. Go ahead and rename it exactly as you see it here, and then we're going to use that in a little bit. All right. So let's go ahead and double-click on our Game State script, and that'll bring it up inside of MonoDevelop. And we need three different variables to get us started on this because the game state is going to handle the actual physical spawning of our player, moving that player to one of the random player spawns that we generate. And then we also need a variable that's going to hold that random player spawn. So to get started, let's go ahead and create that. So we're going to say private, because we don't want any access inside of the Inspector, and we don't need any other functions or any other scripts to have access to this. So we're going to say private GameObject, and the game object that we're looking for is going to be player. And then we also need another private variable-- let's go ahead and tab that over just for cleanliness-- and we'll say private PlayerSpawnController. And that's the script name, and we need to give it a variable name. In this case, it's going to be playerSpawnController, actually, it's playerSpawnCTRL, C-T-R-L, like this. And then we need one more private variable, and that's going to be a game object. And the game object that we're looking for is going to be that random player spawn that we get from the player spawn controller script. So we're going to call this randPlayerSpawn. Perfect. Now, we need to create an awake function, so we're going to say void Awake. And inside of our awake function, we're going to get the game object that is going to be our player and also the component of our player spawn controller. So we need to get that script and tie it to a variable. So first things first, let's say player equals GameObject.FindGameObjectWithTag. And the tag that we're looking for is Player, and we've already set that up. And then we also need the playerSpawnCTRL, and we're looking for GameObject. Whoops, let's make sure that we set that right, GameObject.FindGameObjectWithTag. The tag that we're looking for is going to be Player-- whoops, let's make sure it's uppercase P-- PlayerSpawn C-T-R-L for Control. And then we're going to say dot GetComponent because the player spawn controller script is a component of that game object. And so we're going to say Component, and then give it the name of the script that we're looking for, PlayerSpawnController, and then open/closed parentheses, and end with a semicolon. So now that we have that, now what we can do is we can go down into our Start function. And here what we want to do is we want to set the random player spawn variable. We want to give it the data for that. Now, the data that we want for this is going to come from our player spawn controller, and it's going to come from this function here. And so what I'm going to do is I'm going to call that function through this variable. So I'm going to say randPlayerSpawn equals, and then I'm going to set it equal to whatever that function, the GetRandomPlayerSpawn, gives me, whatever it returns. Now, we haven't created it, or haven't told it to return anything yet, but we'll do that here in just a moment. So we're going to say playerSpawnCTRL.GetRandomPlayerSpawn. Now, you'll notice here it's saying No matches. And with IntelliSense, usually if it says No matches, that means that there's a problem with the scope of our script that we're trying to create here. So let's go ahead and take a look at why that's happening. Well, we're looking at the player spawn controller script, and so over here we're wanting to get this function. Now, we can't call this function from another script because you'll notice that it doesn't say public. OK? So we want to allow that to be called from another function. So now that this is set to public, we can go over to our game state-- let's make sure we save this, so hit Control-S. Let's go over to our game state, and let's type that in again. So we're going to say playerSpawnCTRL.GetRandomPlayerSpawn, and there it shows up. OK? So now we can call that function. And then what it will do from here is it's going to call that, and then it's going to do everything that it sees in here. And then we want it to return a value back to the variable that called that function. And so let's go ahead and set that up here. So let's hit Control-S to save our game state, go back to the player spawn controller. And below our print, we're going to say return, and then what do we want to return here> Well, in this case, we want to return the PlayerSpawnArray. And then what index? So which item in the list do we want to return to that? Because we can't return the entire array. That's not possible. We can only return a single game object that's in that array. So here's where that random spawn ID comes in handy. We don't necessarily know which player spawn we want to return. We want it to be random. So that's why we created this Random Range. And so we're going to put it right in here, where normally we would put something like 0. So if it was returning the index of 0, it would give us the first game object in that array. But instead, we want it to be random. So we're going to say randSpawnID, end with a semicolon, and there we go. So hit Control-S. Now, anytime that you are going to return a game object, or return any information I should say, you need to make sure that that function is of the same type. And so right now we're saying public void. Now, if I hit Control-S, and then I just went into Unity, let's play this just to see what happens. You'll see here that it's saying Cannot implicitly convert void to Unity.GameObject. And so what we'll want to do is we'll want to change that from void to GameObject. Let's hit Control-S to save. Let's go back into Unity. That should disappear. But now we're getting a warning here, and it's saying randPlayerSpawn is assigned but is never used. OK? And so what that means is that we have assigned that, but it's not doing anything. So Unity is intuitive enough to tell us, hey, you've got this information, but it's not being put anywhere. It's not being put to use, and that's a waste. And so what we'll do is we'll create a new function, and this function is going to be void PlayerSpawn. Actually, let's do something a little more descriptive. Let's say SpawnPlayer. Now, whenever we spawn the player, we want the player's position to match the random spawn point's position. OK? So we're going to say player.transform.position equals randPlayerSpawn.transform.position. Perfect. Now, let's also go ahead and print out which spawn point that the player has spawned on. So we're going to say print, and we'll say "You have spawned at". And then we'll add into that the variable name that we're trying to put in, so and that will print that out as a string. And so now that we have that set, we need to actually call this function, which we'll do right underneath here in our Start function. So we're going to say SpawnPlayer. Perfect. Hit Control-S to save that. Let's go into Unity. We should see that warning disappear. Let's hit Play, and you'll notice that the player is spawning in our level. Now, if your player happens to be spawning and falling through the level, a really quick thing that you'll want to check is the position of your player spawns. So as you'll notice, PlayerSpawn 01 through 04, I've set the y value to 2. That's above the ground. So if it's set to something like 0, that's going to spawn your player right in the middle of the floor because it's trying to match the transform positions. So if this is set to 0 for the PlayerSpawn, look at where the transform position is of the player. It's right in the middle, and so he's going to be halfway through the floor. So let's select all of those game objects again. Let's set the y value to 2 so that we ensure that the player is above the floor. Now if we hit Play, you'll see that our player is working as he should. And let's hit Play again. Let's check to see which PlayerSpawn we're spawning at. Now, you'll notice it's exactly the same. Why is it the same? Well, you have to remember that we want to assign the script to the proper place. Now, the game state, because we duplicated it, has the player spawn controller. So let's right-click on it, remove that component. Let's add a new component, which is our script, called Game State. Now, whenever we hit Play, we should see our player spawning in a different place all together. So we're still getting that. Now, we're getting a different number, but why is our player not moving around? Let's make sure that we have our player spawn controller tagged. In this case, we didn't tag that, so let's make sure we do so. Lets hit Play, and now you'll see that we're spawning in a different location. And there's our random spawn. So everything is working as it should. Now what we want to do in our next lesson is we want to move on to scripting out the HUD. So we'll 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.