WEBVTT

00:00.080 --> 00:00.620
Hello guys.

00:00.620 --> 00:01.490
Welcome to the video.

00:01.490 --> 00:06.590
We had quite a good amount of practice with the character, with a rigid body, with the velocity controls,

00:06.590 --> 00:09.920
animator, coroutine and a couple of other things.

00:09.920 --> 00:14.990
Now in this video I want to teach you some other important things we need to learn, and they are especially

00:14.990 --> 00:17.450
helpful for such game as a platformer.

00:17.450 --> 00:21.380
And we're going to start by talking about Singleton, what is it, how to use it.

00:21.380 --> 00:25.370
And also, I think it's a very good time for this because we need to make a game manager and we're going

00:25.370 --> 00:26.870
to use it in the future a lot.

00:26.870 --> 00:30.320
So let's click right button, create C sharp script.

00:30.320 --> 00:33.020
And we're going to type Game Manager.

00:33.020 --> 00:37.070
And because it has this Game manager name it's going to get this cool icon.

00:43.260 --> 00:43.770
Yeah.

00:43.770 --> 00:44.700
Very cool.

00:44.730 --> 00:50.100
Now let's make an empty object and let's name it as Game Manager.

00:50.100 --> 00:55.470
And to make it more recognizable, I'm going to put three stars at the start and three stars in the

00:55.470 --> 00:57.900
end, so it's easy to find in the hierarchy.

00:58.320 --> 01:02.430
Now let's drag this game manager on the game manager and let's do reset.

01:02.670 --> 01:04.980
So this game object will be empty.

01:04.980 --> 01:07.500
Most of the time it's only going to have this script.

01:07.500 --> 01:09.750
Game manager and game manager can be used for.

01:09.750 --> 01:13.440
So many things can be used for getting reference of the player.

01:13.440 --> 01:15.690
Can be used for respawning the player.

01:15.690 --> 01:21.120
Uh, keeping score of the fruits you collected or coins, restarting the game, saving the game.

01:21.120 --> 01:22.860
So many things can be done here.

01:22.860 --> 01:25.620
In this script we're going to start with simple.

01:26.600 --> 01:29.810
We're going to make it accessible to any script in the game.

01:29.810 --> 01:34.730
So any script in the game in the future can just say, game manager, give me this.

01:34.850 --> 01:38.060
To do that, we need to turn this into a singleton.

01:38.060 --> 01:39.350
It is very easy to do.

01:39.380 --> 01:47.090
We just need to type public static game manager instance, and then we need to open a week.

01:47.090 --> 01:51.440
And in the awake we need to type instance equals to this.

01:51.710 --> 01:53.030
Let's save this.

01:53.030 --> 01:53.990
Let's.

01:54.850 --> 02:01.030
Give unity time to recompile everything, and now even the player does not have direct connection to

02:01.030 --> 02:01.960
the game manager.

02:01.960 --> 02:05.500
Player can use game manager for whatever it needs.

02:05.500 --> 02:09.190
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

02:09.190 --> 02:15.070
in the future so we can do this game manager dot instance, and then we can do anything we need from

02:15.070 --> 02:16.000
a game manager.

02:16.000 --> 02:18.520
As an example, let's make a public method.

02:19.940 --> 02:20.930
Public void.

02:20.930 --> 02:23.630
Let's say, uh, score.

02:23.630 --> 02:24.740
I'm just going to name it a score.

02:24.740 --> 02:26.450
It's going to give me debug log.

02:27.110 --> 02:29.180
You have five points.

02:31.230 --> 02:31.920
Simple as that.

02:31.920 --> 02:33.270
So it is a public method.

02:33.270 --> 02:34.680
It is in the game manager.

02:35.610 --> 02:39.000
And again, player is not connected to game manager in any way.

02:39.000 --> 02:40.020
We don't have a reference.

02:40.020 --> 02:45.240
We have nothing and we can do something like this Game Manager instance score.

02:45.240 --> 02:47.280
We can call the method from a game manager.

02:48.080 --> 02:50.150
And it will work just like that.

02:53.400 --> 02:54.690
You can see a message.

02:54.690 --> 02:56.010
You have five points.

02:56.010 --> 02:58.440
And this method directs us to game Manager.

02:58.440 --> 03:00.450
We're going to use this couple of times in the future.

03:00.450 --> 03:05.340
We're going to use this as a main point where we can connect everything that we need to be connected.

03:05.340 --> 03:09.270
Now let's remove this method and let's do something else here.

03:09.270 --> 03:13.950
In the future, we're going to need a reference to the player, and we can get reference to the player

03:13.950 --> 03:15.330
in many different ways.

03:15.330 --> 03:22.020
One of them is to make a public player script player, just going to call it like that.

03:22.020 --> 03:25.200
And then on the game manager over here.

03:25.990 --> 03:27.910
We can assign player.

03:28.240 --> 03:29.020
What is it?

03:29.470 --> 03:30.580
Oh, I remove the method.

03:30.580 --> 03:31.210
Okay.

03:32.000 --> 03:33.980
Let's erase it and save it.

03:37.010 --> 03:40.850
So on the game manager over here, we can assign a player just like that.

03:40.850 --> 03:47.270
And now, whenever we need reference to the player, we can type game manager, dot instance, dot player.

03:47.300 --> 03:48.800
Let's do a quick example.

03:48.800 --> 03:52.130
Why not let's make empty object and let's do new script.

03:52.520 --> 03:54.950
I'm just going to call it new object.

03:55.830 --> 03:57.780
Just an example that we're going to delete later.

03:57.780 --> 03:59.460
So let's do this at the start.

03:59.460 --> 04:05.310
Let's do debug log game manager instance player transform position.

04:05.730 --> 04:07.320
Now we have a third script.

04:07.320 --> 04:13.380
And this script will get position of the player because we have player information saved on the game

04:13.380 --> 04:14.820
manager over here.

04:14.820 --> 04:21.270
And because this game manager is a public static instance which makes it a singleton, we can get position

04:21.270 --> 04:22.080
of the player.

04:22.080 --> 04:23.730
Let's go and try this.

04:28.010 --> 04:28.460
Yeah.

04:28.460 --> 04:31.310
We see a message -14 minus six zero zero.

04:31.310 --> 04:35.120
If we select the player, we can see same numbers over here.

04:35.420 --> 04:36.500
Very cool.

04:36.500 --> 04:38.030
Now let's delete this.

04:38.030 --> 04:41.210
Uh, an object that was just to show you the example.

04:41.210 --> 04:42.830
Let's delete this script.

04:42.950 --> 04:49.130
And we need to go to game manager and make sure that we only have one instance of a game manager in

04:49.130 --> 04:49.820
the game.

04:49.820 --> 04:52.550
To do that, we're going to do a simple check in their wake.

04:52.550 --> 04:59.420
If instance equals to null means we don't have any instance yet, then we can do instance equals to

04:59.420 --> 05:00.380
this.

05:00.380 --> 05:04.850
Then we can do else destroy game object.

05:06.420 --> 05:10.260
Now we're going to save it and we're going to go to unity and try this out.

05:10.290 --> 05:13.890
I'm going to select my game manager and I'm going to click duplicate.

05:13.890 --> 05:15.390
So we have two of them.

05:15.390 --> 05:18.780
Then when we go to play mode you're going to see what is going to happen.

05:19.350 --> 05:19.740
Boom.

05:19.740 --> 05:21.090
One of them was deleted.

05:21.090 --> 05:21.870
Just what we need.

05:21.870 --> 05:26.280
So we only have one game manager in the game at all times.

05:26.280 --> 05:27.300
Very nice.

05:27.300 --> 05:28.530
That's all what we need to do.

05:28.530 --> 05:29.970
Let's delete this extra.

05:29.970 --> 05:33.150
Let's save the project and let's go to the next video.
