WEBVTT

00:00.040 --> 00:02.240
We're going to clean up our code.

00:02.240 --> 00:03.120
So let's do that.

00:03.120 --> 00:05.600
Firstly let's go with this player script.

00:05.600 --> 00:08.200
So I'm going to select my player GameObject in the hierarchy.

00:08.200 --> 00:11.400
And let's open up the player script by double clicking on it.

00:11.400 --> 00:16.760
So instead of using find any object by type we're going to use a singleton pattern.

00:16.760 --> 00:19.960
So basically what we're going to do we're going to go to the top of our class.

00:19.960 --> 00:22.640
And over here we're going to create a variable.

00:22.640 --> 00:24.000
So we're going to make it public.

00:24.000 --> 00:29.840
So we can easily access through any other class or any other script.

00:29.840 --> 00:31.840
And we're going to create a static.

00:31.840 --> 00:36.680
Static basically means that we can simply type the this player class name.

00:36.680 --> 00:40.960
And then we can access any variable that we would like to access.

00:40.960 --> 00:42.720
We can access this dumb height.

00:42.760 --> 00:48.840
We can access this speed through another script or another C sharp class that by directly typing this

00:48.840 --> 00:54.320
player script name and that that is the reason we just using this static.

00:54.320 --> 01:00.320
And we're going to create a variable as the name of our this player script or this player class.

01:00.320 --> 01:06.790
So we're going gonna create a variable, uh, and as the name of our, uh, C class, which is player.

01:06.790 --> 01:11.590
So we're going to create a player and we can call simply this one something like instance.

01:11.590 --> 01:14.670
So we're going to call it instance uh instance.

01:14.670 --> 01:17.030
And we're going to close that up with semicolon.

01:17.030 --> 01:19.710
And we will use this awake function.

01:19.710 --> 01:22.350
So we're going to use this private void awake.

01:22.470 --> 01:28.270
So make sure you just include as I did over here basically this function get call right before this

01:28.270 --> 01:29.230
start function.

01:29.230 --> 01:32.030
And it's basically calls as like a start function.

01:32.030 --> 01:35.590
But it get call right before this start function.

01:35.590 --> 01:37.550
And it just get call one time.

01:37.550 --> 01:43.070
So in this start of the game what we want to check, we want to check if the instance is single or not.

01:43.270 --> 01:49.670
If basically we just want a one player strip in our sample scene or in this game.

01:49.830 --> 01:52.190
So we only want one player skip.

01:52.190 --> 01:55.550
And we don't want to have multiple player skip.

01:55.550 --> 01:56.750
So it's gonna be error.

01:56.750 --> 01:57.630
So that's why.

01:57.710 --> 02:00.190
So we're going to create an instance.

02:00.190 --> 02:00.230
Instant.

02:00.710 --> 02:06.590
So we just created this public static player instant and we will move over to this awake function.

02:06.830 --> 02:11.990
So in the awake function we're going to set instant equal to this.

02:11.990 --> 02:14.470
So we just storing all of the information.

02:14.470 --> 02:17.910
Basically this update function this update function.

02:17.910 --> 02:22.550
Then this should function of this player class into this instance parameter.

02:22.590 --> 02:26.110
As you can see we just set instant equal to this.

02:26.110 --> 02:32.510
So we're storing all of the information about this player class into this instant parameter.

02:32.670 --> 02:37.270
And by now we can extract information by going over to this player script.

02:37.270 --> 02:38.270
And then instant.

02:38.270 --> 02:42.070
And we can extract anything that we would like to extract.

02:42.070 --> 02:46.270
Something like we can extract this health text as well from another class.

02:46.270 --> 02:52.790
If we want this rigid body and anything that uh, anything like that, this start function as well,

02:52.830 --> 02:58.670
as well as any custom function that we have in this player, skip, we can easily access.

02:58.710 --> 03:01.380
But before we set instant equal to this.

03:01.420 --> 03:04.700
We need to check if the script is a one time or not.

03:04.700 --> 03:11.180
Basically, we only want to have one player script in this whole game or in this level one, you can

03:11.180 --> 03:14.100
simply say so we only want one script.

03:14.100 --> 03:17.460
So before we set instant equal to this, we're going to check.

03:17.500 --> 03:24.340
If so, we will check if instant instant is equals equals to null if that is the case.

03:24.340 --> 03:26.380
So we're going to make some curly brackets.

03:26.380 --> 03:33.300
So if instant equal to minus the null then we're going to basically we're going to cut this one which

03:33.300 --> 03:34.700
is instant equal to this.

03:34.700 --> 03:38.700
And we're going to paste inside of this curly brackets.

03:38.700 --> 03:42.020
So we're checking if instance equals equals to null.

03:42.060 --> 03:43.900
Basically means this player script.

03:44.260 --> 03:46.260
Uh this player script is empty.

03:46.260 --> 03:50.340
Basically we don't have any player skip or this player game object.

03:50.340 --> 03:55.260
You can simply say or win in which game object the script is attached.

03:55.580 --> 03:59.860
So in our case the player skip attached to this player game object.

03:59.900 --> 04:03.100
So we're checking if instance equals equals to null.

04:03.140 --> 04:06.100
Mean we don't have any player script.

04:06.100 --> 04:09.220
Then what we're doing we're just setting instance equal to this.

04:09.220 --> 04:16.060
So in that situation we're just storing all of the information about this C-sharp player class into

04:16.060 --> 04:17.660
this instance parameter.

04:17.780 --> 04:25.460
And we can extract info by going over to this instance instance parameter instead of using that which

04:25.460 --> 04:29.340
is fairly taxing, like find or any object by type.

04:29.340 --> 04:31.820
And then you have to pass the script name.

04:31.820 --> 04:36.860
So we can simply just go move over to this player script and then instance.

04:36.860 --> 04:41.980
And we can extract any function or any variable that we're looking for directly.

04:41.980 --> 04:45.740
So that's why we just created public static player instance.

04:45.740 --> 04:48.660
And let's suppose we already have a call player script.

04:48.660 --> 04:54.820
So in that situation what we need to do we need to delete or we need to destroy this instance.

04:54.820 --> 04:56.020
Then we don't want.

04:56.060 --> 04:59.540
So we're going to else else we already have a instance.

04:59.540 --> 05:04.450
Then we're going to destroy the instant or this player class that we're storing information.

05:04.450 --> 05:06.530
We're going to destroy it right away.

05:06.530 --> 05:08.330
So we're going to use this destroy function.

05:08.330 --> 05:09.650
In order to do that.

05:09.650 --> 05:12.810
And in this brackets you need to pass the game object.

05:12.810 --> 05:14.370
So we're going to pass itself.

05:14.370 --> 05:20.170
So with the script this player's script is attached to our player game object.

05:20.170 --> 05:23.290
So the player game object just referencing itself.

05:23.290 --> 05:24.450
So we're just destroying.

05:24.490 --> 05:27.010
And we're going to close that up with semicolon by now.

05:27.010 --> 05:30.650
And then we're going to press down Ctrl S in order to save our script.

05:30.770 --> 05:37.090
So we're just storing basically we just created a variable as the name of our player skip as you can

05:37.090 --> 05:37.570
see.

05:37.570 --> 05:43.330
So and we're storing all of the information about this player class or this player script into this

05:43.370 --> 05:44.890
instance parameter.

05:44.930 --> 05:49.890
As you can see if the instance is equals equals to null basically means empty.

05:50.050 --> 05:52.610
Then we're just setting instance equal to this.

05:52.610 --> 06:00.210
Basically storing all all of the things in this in this instance parameter or this instance parable.

06:00.250 --> 06:03.290
Now we're going to move over to player sorry unity.

06:03.290 --> 06:10.930
And we're going to access the player script or anything that we extracting from this player game object.

06:10.930 --> 06:16.210
So basically this enemy game object we do know it's going to keep track of where player is.

06:16.250 --> 06:18.490
And we're just going over to this player script.

06:18.490 --> 06:22.570
And then we just extracting information about this player game object.

06:22.570 --> 06:25.770
So what we're going to do over here, we're going to move.

06:25.890 --> 06:30.930
We're gonna open up the enemy tool script as well as enemy one script in order to do that.

06:30.930 --> 06:34.050
So we're going to select our enemy to GameObject in the hierarchy.

06:34.050 --> 06:37.970
And let's open up the enemy two script quickly by double clicking on it.

06:37.970 --> 06:44.770
And over here as you can see we're checking if call info then game object dot get component.

06:44.770 --> 06:48.370
And the component we're looking for is not equal to null.

06:48.410 --> 06:51.890
Then what we're doing we're just going over to that player game object.

06:51.890 --> 06:53.650
Basically this player game object.

06:53.650 --> 06:58.250
And then we just going over to this player script of this player game object.

06:58.250 --> 07:01.680
And there we're just calling the function, which is the take damage.

07:01.680 --> 07:04.840
And we do know what we have done inside that function.

07:04.840 --> 07:10.560
So instead of using this long term like this, we're going to simply go to that player skip.

07:10.560 --> 07:13.800
So what we're going to do we're going to go over to that player skip.

07:13.840 --> 07:15.920
So we can simply pass the script name.

07:15.920 --> 07:17.520
So in our case player.

07:17.520 --> 07:20.920
And then we're going to use this dot and we're going to access the instance.

07:20.920 --> 07:27.440
And we do know we just created the instance parameter right there as you seen in our player skip or

07:27.480 --> 07:29.120
that player C class.

07:29.120 --> 07:32.040
And then from there by this instance parameter.

07:32.040 --> 07:33.600
Now we're going to call the function.

07:33.600 --> 07:36.520
And the function we're looking for is a take damage.

07:36.520 --> 07:38.440
So we're going to type this take damage.

07:38.440 --> 07:40.080
And we're going to make first bracket.

07:40.080 --> 07:42.400
And in argument I'm going to pass one.

07:42.400 --> 07:44.480
And let's close that up with semicolon.

07:44.480 --> 07:48.560
So as you can see this seems too much clean by now.

07:48.880 --> 07:55.520
But instead of using this call info that dot GameObject, that dot get component, which is fairly taxing.

07:55.520 --> 07:59.920
So basically we're just going over to we just simply pass the player, script name.

07:59.920 --> 08:05.880
So in our case player as you can see, and then going over to that instance and then calling the Takedamage

08:05.880 --> 08:11.280
function, basically we just going over to this player script and then we're just going over to this

08:11.320 --> 08:12.560
instance parameter.

08:12.560 --> 08:19.000
And by this instance we're just calling this Takedamage function basically as we have done before.

08:19.200 --> 08:20.960
So we're just doing the same thing.

08:20.960 --> 08:27.080
But uh, by but uh, right now we're just using this instance parameter.

08:27.080 --> 08:32.360
So the uh creating singleton pattern makes our code a lot more clean.

08:32.360 --> 08:39.240
And if you haven't grabbed it, uh, perfectly, or if you just want to get it in more details, then

08:39.280 --> 08:46.040
of course I will also gonna give a another video link in the resources so you can check that out about

08:46.080 --> 08:49.120
a singleton pattern if you just want to learn it more.

08:49.240 --> 08:54.880
So we basically storing all of the information about this player class into this instance parameter.

08:54.880 --> 09:00.710
And then we're using this instance parameter in order to call the function, and we use this static.

09:00.710 --> 09:03.150
So we can simply pass the player screen name.

09:03.150 --> 09:09.030
So as you can see we have done over here, which is we just simply pass the player script name.

09:09.030 --> 09:11.110
Then we just moving over to instant.

09:11.110 --> 09:13.630
So make sure you just using that static.

09:13.670 --> 09:16.590
Else you're not gonna able to do like this.

09:16.590 --> 09:22.150
So so you can simply pass the script name without hesitating like that doing anything.

09:22.150 --> 09:27.870
So that's why we just created a static parameter which allow us to use shortly.

09:27.870 --> 09:33.630
Basically, we can just simply type the script name which the, the script that you looking for.

09:33.630 --> 09:37.230
And then you can just do that as I have done over here.

09:37.230 --> 09:40.550
So I'm going to press down Ctrl S in order to save my script.

09:40.550 --> 09:42.390
And now let's move over to unity.

09:42.710 --> 09:47.310
And we're going to do the same thing in my enemy one game object as well.

09:51.590 --> 09:53.270
So just wait for to compile.

09:54.150 --> 10:00.230
And now if I select my enemy one game object I'm going to do the same thing in this anime script, this

10:00.230 --> 10:01.990
anime one script as well.

10:01.990 --> 10:07.350
So I'm gonna open up my anime script by double clicking on it and let's go to the top of our class so

10:07.350 --> 10:09.030
we don't need to do that.

10:09.310 --> 10:11.230
Uh, let's create let's create that.

10:11.230 --> 10:17.390
But before we do, I'm gonna do it, uh, basically over here, as you can see, we're doing basically

10:17.390 --> 10:19.670
we damaging our player over here.

10:19.670 --> 10:24.150
So we're gonna move over to that player class, and then we're going to access the instant.

10:24.150 --> 10:27.230
And by this instant I'm going to call the Takedamage function.

10:27.230 --> 10:28.830
And let's make first bracket.

10:28.830 --> 10:31.150
And then we're going to close that up with semicolon.

10:31.150 --> 10:33.750
And also we need to pass the argument.

10:33.750 --> 10:38.470
So basically we're just going over to this player script and then going over to this instant.

10:38.470 --> 10:44.950
And by using this instant we also call it the Takedamage function in that enemy one script as well.

10:44.950 --> 10:50.230
So I'm going to move over to anime script one more time, and let's press down Ctrl S in order to save

10:50.230 --> 10:50.870
the script.

10:50.870 --> 10:52.790
And now let's move over to unity.

10:52.790 --> 10:59.540
So you can approach the same thing with this enemy one game object, as well as with this two game object

10:59.540 --> 11:05.660
as well, so you can simply create singleton pattern in this enemy two script as well as the enemy one

11:05.660 --> 11:06.220
script.

11:06.220 --> 11:07.780
So you're gonna have that.

11:07.780 --> 11:10.220
So before we do, let's have a look by now.

11:10.220 --> 11:11.940
So I'm going to click on this play button.

11:12.140 --> 11:15.060
There should be no errors in our console tab.

11:15.060 --> 11:17.900
So before we do let's have a look on this.

11:17.940 --> 11:20.860
And we're gonna do the same thing in this enemy two game object.

11:20.860 --> 11:24.340
Basically enemy two script as well as enemy one script.

11:24.340 --> 11:27.340
So we're not able to see any error in the console.

11:27.500 --> 11:31.900
So you can just ignore that error, which is not at all thing.

11:31.940 --> 11:35.460
And we can see the player game object indeed taking damage.

11:35.460 --> 11:39.740
And once it loses all of his health, we can see a message in the console tab.

11:39.740 --> 11:40.300
Just kidding.

11:40.300 --> 11:46.620
Pop up says player died because we do know once our player game object loses all of his health, then

11:46.620 --> 11:50.900
we're just calling the type function and we do know what type function really doing.

11:50.900 --> 11:53.140
So I'm going to get out of play mode by now.

11:53.140 --> 11:58.180
I hope you get the idea that how you can do it, and I'm going to do the same thing in this enemy anime

11:58.180 --> 11:59.340
to skip as well.

11:59.660 --> 12:04.620
So I'm going to open up my anime script and let's go to top of our class and let's make it public.

12:04.620 --> 12:11.300
So make sure you making it public so you can just access through another class or another script easily.

12:11.420 --> 12:15.540
And we're going to create a static parameter and we're going to call it anime two.

12:15.580 --> 12:21.740
Basically we're just creating a variable as the name of our script or this C class, and we're going

12:21.780 --> 12:24.100
to call it instant as we have done.

12:24.260 --> 12:25.660
And we're going to set.

12:25.700 --> 12:28.060
So let's use the awake function.

12:28.060 --> 12:30.980
Or you can use the start function as well.

12:30.980 --> 12:32.500
It's totally up to you.

12:32.500 --> 12:37.580
So in this script or this anime script I'm going to use the start function.

12:37.580 --> 12:41.060
And we do know start function, get call and the start of the game.

12:41.060 --> 12:45.140
And in the start of the game I'm going to check if instant is equals.

12:45.140 --> 12:47.220
Equals to null means empty.

12:47.340 --> 12:49.940
Then we're going to set instant equal to this.

12:49.940 --> 12:52.540
So we're going to set instant equal to this.

12:52.580 --> 12:55.100
Else we're going to destroy our instant.

12:55.100 --> 12:56.980
So we're going to use this else condition.

12:56.980 --> 13:00.530
And we're going to use this destroy function in order to destroy.

13:00.530 --> 13:02.570
So we're going to destroy this.

13:02.570 --> 13:04.610
And let's close that up with semicolon.

13:04.850 --> 13:07.770
And let's do the same thing in our anime one script.

13:07.770 --> 13:15.810
So I'm going to move over to my anime script or the first anime and let's create a another public static.

13:16.130 --> 13:17.810
And this will be enemy.

13:17.850 --> 13:19.970
And I'm going to call this one instant.

13:20.330 --> 13:22.570
And let's go to start function.

13:22.810 --> 13:25.930
Uh, for this script I'm going to use the awake function.

13:25.930 --> 13:32.250
So let's use this awake and this awake function get call uh, in the start of the game right before

13:32.250 --> 13:33.530
this start function.

13:33.530 --> 13:36.570
And it also get one time just one time.

13:36.730 --> 13:41.810
And we're gonna check if instance equals equals to null.

13:42.090 --> 13:47.090
Then we're going to set instant equal to this instant equal to this.

13:47.090 --> 13:48.410
So let's set that.

13:48.410 --> 13:55.410
And if in already there is an instant then we're going to destroy this instant this.

13:55.450 --> 13:56.770
And that's it.

13:57.050 --> 14:02.650
And I'm going to move over to my player script where we destroying or just damaging our player.

14:02.650 --> 14:05.050
So I'm going to move over to my player game object.

14:05.050 --> 14:11.810
So over here, as you can see we just are damaging our player and we're just using fairly taxing or

14:11.850 --> 14:17.210
heavy texting, heavy text in order to do that or in order to damage our enemy.

14:17.210 --> 14:23.170
So instead of using this, we can simply go to enemy to script, or we can simply pass the script name.

14:23.170 --> 14:25.810
So in our case this enemy to a.

14:25.970 --> 14:27.570
So we're going to go to instant.

14:27.570 --> 14:29.970
And now we're going to call that takedamage function.

14:29.970 --> 14:33.330
And let's make first bracket and then close that up with semicolon.

14:33.330 --> 14:38.930
And once I damaging enemy one then I'm going to use this enemy one script enemy.

14:38.930 --> 14:40.850
Then we will move over to instant.

14:40.850 --> 14:43.250
And we're going to call that Takedamage function.

14:43.250 --> 14:45.810
And over here it's going to take the argument.

14:45.810 --> 14:47.090
And I'm going to pass one.

14:47.090 --> 14:49.930
So it will just going to subtract by one unit.

14:50.450 --> 14:54.090
And I'm going to press down control S in order to save script.

14:54.090 --> 14:57.840
And let's do the same thing with this ground spawner as well.

14:57.840 --> 14:59.000
So it's going to be fine.

14:59.200 --> 15:00.800
So I'm going to move over to.

15:00.840 --> 15:02.120
My over here.

15:02.520 --> 15:06.520
Let's go to top of our class and let's make a public static.

15:06.840 --> 15:10.920
So let's make a public static that will be ground spawner.

15:10.920 --> 15:13.840
And I'm going to call this one instant as well.

15:14.080 --> 15:18.960
And for this script or this class I'm going to use this start function.

15:18.960 --> 15:23.360
And I'm going to check if instance x equals equals to null.

15:23.480 --> 15:26.120
Then we're going to set instant equal to this.

15:26.440 --> 15:29.120
So instant equal to this.

15:29.160 --> 15:31.200
Else we're going to destroy that.

15:31.200 --> 15:33.240
So let's use this else condition.

15:33.480 --> 15:33.960
Else.

15:34.000 --> 15:39.200
If already there is an instant then we're going to destroy this instant.

15:39.200 --> 15:40.800
So we will destroy this.

15:40.800 --> 15:42.680
And let's close that up with semicolon.

15:43.000 --> 15:45.880
And I'm going to move this curly brackets a little bit up.

15:46.120 --> 15:51.360
And let's move over to player script where basically we're just calling the ground spawner function

15:51.360 --> 15:53.200
in order to spawn our ground.

15:53.200 --> 15:54.400
So let's go there.

15:54.400 --> 15:59.440
And over here, as you can see we are we're using this find any object by type.

15:59.440 --> 16:04.480
And then we're just going over to ground spawner script and then calling the spawn ground function,

16:04.480 --> 16:07.360
which is fairly taxing as you can see.

16:07.360 --> 16:14.480
So instead of doing this, we can simply, uh, now we can simply pass our ground spawner as class name.

16:14.800 --> 16:17.040
And then we're going to move over to instant.

16:17.040 --> 16:19.760
And now we're going to call the spawn ground function.

16:19.760 --> 16:21.080
And let's make first bracket.

16:21.080 --> 16:23.080
And let's close that up with semicolon.

16:23.080 --> 16:25.960
And then we're going to press down Ctrl S to save our script.

16:25.960 --> 16:28.000
And let's move to unity by now.

16:30.320 --> 16:32.000
So just wait for to compile.

16:32.000 --> 16:36.120
And once it's done we are ready to do that basically.

16:36.160 --> 16:38.760
Basically we just calling those function.

16:38.760 --> 16:39.760
So just wait.

16:41.080 --> 16:42.760
And now it should be fine.

16:42.760 --> 16:44.840
And you can ignore this error.

16:44.960 --> 16:47.760
It's not much at all effective.

16:47.760 --> 16:49.480
So we can simply clear that.

16:49.480 --> 16:51.720
And let's click on this play button in the top.

16:51.720 --> 16:53.400
And let's have a look on this.

16:53.440 --> 16:55.720
If it's perfectly working or not.

16:55.720 --> 16:57.710
So we're going to take a look on this.

16:57.710 --> 17:01.270
So we're using singleton pattern in order to do that.

17:01.270 --> 17:05.710
And if you still a little bit worried about a singleton pattern.

17:05.710 --> 17:12.510
And if you just didn't understand it well uh, I will have a video in the resources, a link, you can

17:12.510 --> 17:20.350
go and check that out about, uh, singleton pattern that we use in this, uh, our project.

17:20.350 --> 17:22.710
So you can take a look on that.

17:22.870 --> 17:29.310
I hope you get the idea that how you can use singleton pattern in order to make your code clean, and

17:29.310 --> 17:29.990
that's it.

17:30.030 --> 17:32.030
Uh, that that's pretty fine.

17:32.030 --> 17:37.150
And also, we need to adjust our background perfectly in order to seamlessly.

17:37.150 --> 17:43.830
It's gonna, uh, see we're gonna see that it just, uh, spawning, uh, top of our, this background.

17:43.830 --> 17:50.150
So it should, uh, so we're going to, uh, take a look on this, uh, thing in the end of the, our

17:50.150 --> 17:50.630
lecture.

17:50.630 --> 17:52.270
So I'm going to get out of play mode.

17:52.310 --> 17:53.590
I hope you get the idea.

17:53.590 --> 17:57.470
And I'm going to press down Ctrl s in order to save my project.
