Check out a free preview of the full Game Development with Unity for Web Developers course

The "Resetting Game State" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick walks through adding the functionality to restart the game once it has ended.

Preview
Close

Transcript from the "Resetting Game State" Lesson

[00:00:00]
>> There's one more thing we need to do in the GameManager, this is our longest script. We need to add a method to restart the game once it's over. So, just like in other scripts, we need a RestartGame method. And if the game is not playing and RestartGame is called, then we can unpause the game by setting the time scale to 1, because remember we haven't done that anywhere yet, we've kept setting it to 0.

[00:00:30]
We'll set it to 1, we'll reset the game timer and we'll say that gameIsPlaying is true. So, let's write that up. So, public void RestartGame. We'll say if, The gameIsnotPlaying, Then unpause the game by setting the time scale to 1. So, Time.timeScale should be 1, and then reset the game timer and that will say timer is equal to gameTimeInSeconds.

[00:01:19]
So we're resetting the game clock to 60 seconds, and then gameIsPlaying should be true. So, we've added a couple of things here. We have our RestartGame method, we have a method that gets called if the game is over. There's a FixedUpdate that's running to decrement the timer. And then in our Start method, we're setting the game time at the start and updating the timer in seconds.

[00:01:58]
And the reason we do this here is because this UpdateTimer method is going to call some UI methods. So we wanna do it here just before everything starts so that the UI looks nice at the start of game. So, let's save our GameManager, and we're gonna switch over to Unity.

[00:02:22]
And now, it's time to actually create an opponent. So, we have our player, switch back to the scene view, but we never created an opponent. So, we want that opponent to be just opposite the player, and we can do that by duplicating the player. So on my Mac, I just selected the player and hit Command D to duplicate, and I'm going to change the name of this game object to opponent.

[00:02:58]
And just for the sake of clarity, I'm going to rearrange it in the hierarchy just so it's next to the player, that's not actually necessary, but it's just some good organization. And so where do we wanna put this opponent? Well, we wanna put it on the other side of the game board here, right?

[00:03:19]
Well, the player was at negative 3, that's what our opponent is at, we wanna put this at positive 0.3, all right? So we have the player negative 0.3 of the opponent positive 0.3. And the opponent is gonna have all the same stuff as the player for the most part.

[00:03:43]
It still needs a sphere collider, it still needs a rigid body, it still needs the rolling movement script, it still needs score, but we don't want this to accept input, so we're going to remove the player input component from the opponent. And we're going to remove the player movement script because we'll replace that with another script that will drive rolling movement.

[00:04:15]
So, let's switch over to the player. And now that we have all of those restart methods in place, we can actually add the rest of our input callback methods. So, under the player inputs component, we're going to add several things to restart here. I'm gonna switch back to the slides just so we can go over what those are really quickly.

[00:04:50]
So, we have PlayerMovement.OnMove, we have that hooked up already, and Unity. But we need to add six methods to restart, there's a lot of stuff to restart when the game gets restarted, when we hit the spacebar. And one of those is the method we just wrote in GameManager.RestartGame, we need to reset the position for the rolling movement components on both the player and the opponent.

[00:05:25]
We need to reset the position of the collectible, so it's back in the middle of the board. And then we need to reset the score on both the player and the opponent. So, there's those six things we need to hook up. So, let's switch back to Unity and do each one of those.

[00:05:45]
So, I'll come down here, and here is the restart callback context, just to review where this is at, we're on the player game object. We're looking at the player input component. And then I'll fold this back up because this is how it is by default, under Events, Player, Restart is where we want to add these.

[00:06:14]
So, we need six of them. So, I'll hit this little plus button six times, 1, 2, 3, 4, 5, 6. And the first one that we need here is the GameManager. Now, previously, I was just hitting this little target icon and choosing from the menu of things that are either in the scene or assets that are in the project.

[00:06:40]
There's another way you can do this, you can actually just drag game objects from the hierarchy or drag things from the project window into the slots. Now, this is something that tends to trip beginners a little bit, because we need to add the GameManager to one of these slots so that we can call RestartGame on the Game Manager.

[00:07:03]
Think about this for a second, we could grab the script here and put it into the slot but that's not going to do what we want because that is going to only be able to access static methods that are on the class. It's not going to be able to access the instance of the GameManager that is in the actual scene, okay?

[00:07:27]
So we wanna make sure we're dragging from the hierarchy in this case to this slot. And the same thing is true when you click this button, so if I were to type in GameManager, right? You could get it from assets, that would be incorrect though we actually want it from the scene.

[00:07:51]
So, let's drag the GameManager over to there, and then the next thing we need is the Player, and we'll call a few different methods on that. There's the opponent, we need the collectible. And then one more time we need both the Player and the Opponent. So, let's set up each one of these methods.

[00:08:28]
On the GameManager, we want to call the public method Restartgame. On the Player, we need to call on RollingMovement, ResetPosition. On the opponent, we need to call RollingMovement, ResetPosition. On the Collectible, we need to call ResetPosition. And then finally on the Player and Opponent, we need to call Score.ResetScore.

[00:09:12]
So now, if we save the scene, When I hit the play button nothing should happen. And I was fooled because there's no UI here yet, but this is doing exactly what we told it to be doing, the time scale set to 0 until we hit the space bar, and then the game starts.

[00:09:37]
So of course, I can still move around. The Opponent is there, but the Opponent isn't really going to do anything because we haven't written any scripts that are going to drive its RollingMovement script just yet.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now