Hello guys.
Welcome to the video.
We had quite a good amount of practice with the character, with a rigid body, with the velocity controls,
animator, coroutine and a couple of other things.
Now in this video I want to teach you some other important things we need to learn, and they are especially
helpful for such game as a platformer.
And we're going to start by talking about Singleton, what is it, how to use it.
And also, I think it's a very good time for this because we need to make a game manager and we're going
to use it in the future a lot.
So let's click right button, create C sharp script.
And we're going to type Game Manager.
And because it has this Game manager name it's going to get this cool icon.
Yeah.
Very cool.
Now let's make an empty object and let's name it as Game Manager.
And to make it more recognizable, I'm going to put three stars at the start and three stars in the
end, so it's easy to find in the hierarchy.
Now let's drag this game manager on the game manager and let's do reset.
So this game object will be empty.
Most of the time it's only going to have this script.
Game manager and game manager can be used for.
So many things can be used for getting reference of the player.
Can be used for respawning the player.
Uh, keeping score of the fruits you collected or coins, restarting the game, saving the game.
So many things can be done here.
In this script we're going to start with simple.
We're going to make it accessible to any script in the game.
So any script in the game in the future can just say, game manager, give me this.
To do that, we need to turn this into a singleton.
It is very easy to do.
We just need to type public static game manager instance, and then we need to open a week.
And in the awake we need to type instance equals to this.
Let's save this.
Let's.
Give unity time to recompile everything, and now even the player does not have direct connection to
the game manager.
Player can use game manager for whatever it needs.
I'm just going to give you an example on how it's going to work, and we're going to use this a lot
in the future so we can do this game manager dot instance, and then we can do anything we need from
a game manager.
As an example, let's make a public method.
Public void.
Let's say, uh, score.
I'm just going to name it a score.
It's going to give me debug log.
You have five points.
Simple as that.
So it is a public method.
It is in the game manager.
And again, player is not connected to game manager in any way.
We don't have a reference.
We have nothing and we can do something like this Game Manager instance score.
We can call the method from a game manager.
And it will work just like that.
You can see a message.
You have five points.
And this method directs us to game Manager.
We're going to use this couple of times in the future.
We're going to use this as a main point where we can connect everything that we need to be connected.
Now let's remove this method and let's do something else here.
In the future, we're going to need a reference to the player, and we can get reference to the player
in many different ways.
One of them is to make a public player script player, just going to call it like that.
And then on the game manager over here.
We can assign player.
What is it?
Oh, I remove the method.
Okay.
Let's erase it and save it.
So on the game manager over here, we can assign a player just like that.
And now, whenever we need reference to the player, we can type game manager, dot instance, dot player.
Let's do a quick example.
Why not let's make empty object and let's do new script.
I'm just going to call it new object.
Just an example that we're going to delete later.
So let's do this at the start.
Let's do debug log game manager instance player transform position.
Now we have a third script.
And this script will get position of the player because we have player information saved on the game
manager over here.
And because this game manager is a public static instance which makes it a singleton, we can get position
of the player.
Let's go and try this.
Yeah.
We see a message -14 minus six zero zero.
If we select the player, we can see same numbers over here.
Very cool.
Now let's delete this.
Uh, an object that was just to show you the example.
Let's delete this script.
And we need to go to game manager and make sure that we only have one instance of a game manager in
the game.
To do that, we're going to do a simple check in their wake.
If instance equals to null means we don't have any instance yet, then we can do instance equals to
this.
Then we can do else destroy game object.
Now we're going to save it and we're going to go to unity and try this out.
I'm going to select my game manager and I'm going to click duplicate.
So we have two of them.
Then when we go to play mode you're going to see what is going to happen.
Boom.
One of them was deleted.
Just what we need.
So we only have one game manager in the game at all times.
Very nice.
That's all what we need to do.
Let's delete this extra.
Let's save the project and let's go to the next video.