WEBVTT

00:00.040 --> 00:06.400
We're going to create a script in order to basically if we click on this retry button, then we want

00:06.400 --> 00:07.720
to reload the level.

00:07.720 --> 00:14.280
And if we click on this menu button, then we just want to move over to our panel scene or menu.

00:14.680 --> 00:18.160
And we don't have the menu yet, so we're going to create it later.

00:18.160 --> 00:20.400
For now we're going to print out a message.

00:20.400 --> 00:24.560
But before we do, let's create the script in order to load our level.

00:24.600 --> 00:29.360
To do so, let's head over to our project App assets and then script folder.

00:29.360 --> 00:33.160
Let's right click there create and then move over to scripting.

00:33.160 --> 00:35.680
And then click on this Monobehaviour script.

00:35.720 --> 00:40.720
Or you can simply click on this Monobehaviour script in the top, as you can notice.

00:40.720 --> 00:46.880
And you can call that script to something like scene management or something like Game Manager as well.

00:46.920 --> 00:51.880
I already created a script called Scene Management, as you can notice right over here.

00:51.880 --> 00:58.480
So we need to attach this script and to any GameObject in our hierarchy to make it work.

00:58.520 --> 01:03.750
Or we can simply create another GameObject for this script separate game object.

01:03.750 --> 01:05.430
So let's do that for it.

01:05.430 --> 01:07.190
So I'm going to move over to hierarchy.

01:07.190 --> 01:09.350
Let's right click or hit this plus icon.

01:09.350 --> 01:11.270
Let's create an empty game object.

01:11.270 --> 01:14.790
And let's call this game object to something like scene management.

01:14.790 --> 01:19.150
So I'm going to call this one scene management.

01:19.470 --> 01:22.350
And let's go over to Inspector Transform.

01:22.350 --> 01:24.670
Let's right click on it and then reset.

01:24.830 --> 01:27.230
And I'm going to select my scene Management game object.

01:27.230 --> 01:31.710
And I'm going to drag it in the top of my hierarchy so I can see all the time.

01:31.710 --> 01:34.390
And we're going to select this scene management script.

01:34.390 --> 01:38.510
And let's drag it over to this Add Component section in Inspector.

01:38.510 --> 01:42.350
And let's open up our scene management script by double clicking on it.

01:42.550 --> 01:45.950
And let's remove this update and start function.

01:45.950 --> 01:52.430
We don't need uh, so whenever we working with scene, make sure you just go to top of your class and

01:52.430 --> 01:53.790
include this namespace.

01:53.790 --> 01:59.710
And it's called using Unity engine Unity engine dot scene management.

01:59.710 --> 02:02.030
So make sure you include this namespace.

02:02.270 --> 02:10.500
Uh just move over to top of your class and include this namespace called using Unity Engine dot Scene

02:10.540 --> 02:13.020
Management, because we're gonna work with scene.

02:13.020 --> 02:13.820
So that's why.

02:14.100 --> 02:18.820
And we need to make this function public since we're going to call this function through a button.

02:18.820 --> 02:27.060
So if I move over to unity and then go over to canvas, then UI or this BG basically, and then select

02:27.060 --> 02:30.900
this retry button or this menu button, then go over to Inspector.

02:30.900 --> 02:31.780
Scroll down.

02:31.820 --> 02:33.420
We have a button component.

02:33.420 --> 02:35.780
And in the bottom we can see on click.

02:35.780 --> 02:40.020
So we can call a function over here in order to do something.

02:40.020 --> 02:44.940
So that's why we need to make those function public so we can just access it over here.

02:44.940 --> 02:49.700
So whenever we're going to click on this retry function add a retry button.

02:49.700 --> 02:51.900
Then that function just gonna trigger.

02:51.900 --> 02:52.740
So that's why.

02:52.940 --> 02:54.940
So let's move over to our scape.

02:54.940 --> 02:56.900
So we're going to create public.

02:57.180 --> 02:59.660
And for function we're going to type void.

02:59.700 --> 03:01.580
And we have to name the function.

03:01.580 --> 03:08.680
So I'm going to call this function to something like retry retry level, something like that.

03:08.680 --> 03:11.600
And let's make first bracket and some curly brackets.

03:11.760 --> 03:17.640
So inside this function what we want to do we just want to reload the scene which we are currently in

03:17.800 --> 03:20.240
or which level we are currently in.

03:20.720 --> 03:23.920
So we are currently in this uh higher.

03:24.120 --> 03:28.840
If we move over to hierarchy, we can see the name of the scene is Sample Scene.

03:29.000 --> 03:35.600
And if you want to see the build index of it then what you can do you can go to top left in unit editor

03:35.600 --> 03:36.440
says file.

03:36.640 --> 03:41.320
Then click on this build profiles in the bottom and you can see the name of the scene.

03:41.320 --> 03:43.840
So right over here it's going to be Scene List.

03:43.840 --> 03:46.160
And then click on this open scene list.

03:46.160 --> 03:49.200
And you can see the name of the scene and the build index.

03:49.200 --> 03:52.120
We can notice in the right section which is zero.

03:52.120 --> 03:56.320
So we can simply pass the name of the scene or the build index of it.

03:56.320 --> 03:58.640
So I'm going to cut this step nine right now.

03:58.640 --> 04:00.760
And let's move over to our script.

04:00.760 --> 04:05.040
And in order to load the level we need to use this scene manager.

04:05.200 --> 04:07.710
Scene manager dot load scene function.

04:07.710 --> 04:13.590
So make sure you use this function in order to load the scene or the level that you want to load up.

04:13.790 --> 04:15.790
So scene manager dot load scene.

04:15.790 --> 04:23.430
And inside this uh first bracket, we can pass the name of the scene in quotation or the index of it

04:23.630 --> 04:24.830
without quotation.

04:25.030 --> 04:27.030
But we're gonna use something else.

04:27.030 --> 04:30.630
So we're gonna use this scene manager as scene manager.

04:30.830 --> 04:32.590
Dot get active scene.

04:32.910 --> 04:37.430
Then we're going to make this first bracket, and then we're gonna use this dot, and we're going to

04:37.430 --> 04:41.350
pass the index of it and then close that output semicolon.

04:41.350 --> 04:45.430
So what basically this means scene manager dot get active scene.

04:45.670 --> 04:49.830
The active scene basically means the scene which we are currently in.

04:50.030 --> 04:57.270
So we are if we move over to unity, we can see we uh, we currently in our, uh, sample scene and

04:57.270 --> 05:00.670
then what we're doing, we just accessing the index of it.

05:00.670 --> 05:06.550
And if we move over to unity, then top left file, then build profiles so we can see the name of the

05:06.550 --> 05:07.870
scene is sample scene.

05:07.870 --> 05:11.900
in and in the right section we can see the build index is zero.

05:12.100 --> 05:15.500
So over here it's just going to be zero right away.

05:15.540 --> 05:15.900
Why.

05:16.100 --> 05:18.100
Because scenemanager dot get feet.

05:18.140 --> 05:23.980
SIM basically means the scene which we are currently in or which level we are currently in.

05:24.020 --> 05:26.740
And then we accessing the build index of it.

05:26.740 --> 05:29.260
And we saw the build index is zero.

05:29.300 --> 05:31.340
The scene which we are currently in.

05:31.500 --> 05:39.500
And we do know currently in basically the scene is sample scene and the build index of it is zero.

05:39.500 --> 05:45.540
So basically means this scene manager dot load scene function is going to load the scene which we are

05:45.540 --> 05:46.500
currently in.

05:46.580 --> 05:49.460
Basically it's going to load up the samples in itself.

05:49.620 --> 05:51.420
So I hope you get the idea.

05:51.500 --> 05:56.260
And now we're going to press down Ctrl s on our keyboard in order to save our script.

05:56.260 --> 05:58.700
And then we're going to move over to unity.

05:59.220 --> 06:06.180
And we need to hook this retry button with that retry retry level function.

06:06.300 --> 06:07.620
So just wait a moment.

06:08.260 --> 06:15.490
And once our compilation is done, we can just go ahead and we can hook this retry button with our retry

06:15.530 --> 06:16.410
level function.

06:16.530 --> 06:20.050
Now the, uh, now, uh, the script gets compiled.

06:20.130 --> 06:26.290
So now what we have to do, we need to move over to hierarchy, then canvas game over ui bg.

06:26.290 --> 06:28.450
And then let's select this retry button.

06:28.450 --> 06:29.890
Let's go over to Inspector.

06:29.890 --> 06:34.770
Scroll down to the button component and we can call the function over here on click.

06:34.770 --> 06:37.810
So we need to click on this plus icon.

06:37.810 --> 06:43.050
And we need to drag the game object in which game object the script is sitting on.

06:43.050 --> 06:48.370
So if I select my Scene Management GameObject in the hierarchy and then go over to Inspector, we can

06:48.370 --> 06:52.810
notice Scene Management Script is sitting on this scene Management game object.

06:52.810 --> 06:56.090
So that's why we need to select this scene management game object.

06:56.090 --> 06:59.530
And we need to drag it over here and it says no function.

06:59.530 --> 07:05.210
So just click there and we will go over to scene management script or Scene Management class.

07:05.210 --> 07:08.970
And in there we can see we created the retry level function.

07:08.970 --> 07:10.170
So we're going to choose that.

07:10.210 --> 07:15.510
It It's basically means as soon as we're going to click on this retry function, then that function

07:15.510 --> 07:17.390
is just going to call out which retry.

07:17.430 --> 07:23.230
So basically we're just moving over to this scene management class or this script.

07:23.230 --> 07:26.910
And then we just call it we just triggering this function.

07:26.910 --> 07:32.510
Basically whenever we're going to click on that retry button then this function just gonna get called.

07:32.510 --> 07:37.550
And what this function is really doing, it's just loading the scene which we are currently in.

07:37.590 --> 07:38.750
As you can notice.

07:38.910 --> 07:41.390
Now we're gonna move over to unity.

07:41.630 --> 07:43.990
And let's click on this play button in the top.

07:43.990 --> 07:45.910
And let's have a look on this.

07:45.910 --> 07:51.750
So as soon as we're going to click on that retry button then retry level a function just going to call

07:51.750 --> 07:52.070
out.

07:52.070 --> 07:56.430
And we do know what we're doing inside that retry level function.

07:56.430 --> 08:01.070
So just wait a moment and we're going to take damage from this.

08:01.110 --> 08:03.990
You can simply say this show or this fire.

08:04.310 --> 08:10.270
So as you can see our retry basically this BG just pops up.

08:10.430 --> 08:14.140
And if I just go ahead and then click on this retry button.

08:14.140 --> 08:19.740
As you can notice, as soon as we click we just loaded the scene which we are currently in.

08:19.740 --> 08:24.780
So we are currently in sample scene and we just accessing the build index of it.

08:24.780 --> 08:27.380
And we do know the build index is zero.

08:27.380 --> 08:30.580
So basically just it just reloading itself.

08:30.940 --> 08:33.020
And let me show you one more time.

08:33.020 --> 08:34.860
So let me show you.

08:36.220 --> 08:41.700
And now we can just click on it retry button in order to reload the level.

08:41.700 --> 08:45.060
And if we just go ahead and then click on this menu button.

08:45.100 --> 08:49.980
This button is doing nothing currently and we don't have the menu scene yet.

08:49.980 --> 08:55.380
So for now we're gonna print out a message if this button is working perfectly or not.

08:55.380 --> 08:57.180
So we're gonna check that out.

08:57.180 --> 09:01.220
And if we click on this retry button then we just reloading the level.

09:01.220 --> 09:03.540
So we're gonna get our get out of play mode.

09:03.540 --> 09:07.100
And now let's create another function for this menu button.

09:07.100 --> 09:11.180
So let's move over to our scene management class or script.

09:11.180 --> 09:14.410
And let's make another function for that menu button.

09:14.410 --> 09:17.570
So we're going to make public void for function.

09:17.570 --> 09:19.690
And we're going to call this a load menu.

09:19.690 --> 09:22.090
Or let's simply call it menu.

09:22.130 --> 09:24.850
Let's make first bracket and some curly brackets.

09:24.850 --> 09:26.970
So we don't have the menus in yet.

09:26.970 --> 09:31.370
So if this button is working or not we're going to print out a message.

09:31.370 --> 09:34.930
So I'm going to type this Debug.log for printing out message.

09:34.930 --> 09:38.050
And we're going to say something like uh load menu.

09:38.050 --> 09:42.010
So let's say something like load menu and load menu.

09:42.010 --> 09:44.170
And let's close that up with semicolon.

09:44.170 --> 09:44.650
Sorry.

09:45.010 --> 09:47.410
We're going to close that off with a semicolon.

09:47.410 --> 09:50.210
And we're going to press down Ctrl S to save our script.

09:50.210 --> 09:52.130
And now let's move over to unity.

09:52.370 --> 09:56.010
So we need to hook that menu function with our menu button.

09:56.010 --> 10:00.610
So as soon as we're going to click on this menu button that function just gonna trigger.

10:00.610 --> 10:06.250
And what that function is really doing is just printing out the message in the console says load menu.

10:06.290 --> 10:08.850
As you can notice inside this function.

10:08.850 --> 10:13.050
So we're going to select our console tab in order to see the message.

10:13.050 --> 10:14.920
So just wait a little moment.

10:14.920 --> 10:17.040
So now if I select my console tab.

10:17.320 --> 10:23.120
But before we do, make sure you select your menu button underneath your game object, underneath your

10:23.160 --> 10:24.320
Game Over UI.

10:24.440 --> 10:27.920
Then go over to Inspector and make sure you select the menu button.

10:27.920 --> 10:29.360
Then go over to Inspector.

10:29.520 --> 10:33.440
Scroll down to the button component and then click on this plus icon.

10:33.440 --> 10:37.600
And we do know that we need to drag the game object in which game object the.

10:37.680 --> 10:40.760
This basically script is sitting on.

10:40.760 --> 10:42.720
So in our case scene management.

10:42.720 --> 10:46.040
So we're going to select that and let's drag it over to that field.

10:46.040 --> 10:47.400
And it says no function.

10:47.400 --> 10:52.480
So let's click there and let's go to scene management script or scene management class.

10:52.480 --> 10:55.440
And we can see the menu function right over here.

10:55.440 --> 10:58.480
So we're going to choose this menu function for this menu button.

10:58.480 --> 11:03.520
So as soon as we're going to click on this menu button that menu function just gonna trigger.

11:03.520 --> 11:05.480
And what this menu function is doing.

11:05.480 --> 11:10.480
It's basically printing out the message in the console says load menu as you can notice.

11:10.480 --> 11:13.960
So now let's move over to unity and let's select the console tab.

11:13.960 --> 11:18.420
And let's click on this play button in the job in order to play our game.

11:18.420 --> 11:19.580
And let's take a look.

11:19.580 --> 11:24.900
So as soon as we're going to click on that menu button, that menu function just gonna trigger and that

11:24.900 --> 11:27.980
menu function basically just loading up the message.

11:28.140 --> 11:28.620
Sorry.

11:28.740 --> 11:31.260
Printing out the message says load menu.

11:31.380 --> 11:36.540
So let's take damage from this or this wraps first.

11:39.060 --> 11:42.020
And we can notice it gets pop up.

11:42.020 --> 11:45.620
So now if I just go ahead and then click on this menu button.

11:45.740 --> 11:51.420
As you can see as soon as we're clicking this menu button, if we move over to our console tab, we

11:51.420 --> 11:54.380
can see a message just getting pop up says menu.

11:54.380 --> 11:59.940
So we basically are printing out this message to check if this button is working or not.

11:59.980 --> 12:02.420
Because we don't have the menus in yet.

12:02.460 --> 12:03.620
We need to create.

12:03.620 --> 12:06.380
So that's why for now we just printing out the message.

12:06.380 --> 12:12.140
So as soon as we're clicking on this menu button, that menu function just calling and what that menu

12:12.140 --> 12:18.090
function is doing, it's basically the printing out the message load menu in our console tab, As you

12:18.090 --> 12:18.810
can notice.

12:18.810 --> 12:24.290
So I hope you get the idea that how you can do it and we're gonna get out of play mode and make sure

12:24.290 --> 12:25.850
you select the parent game object.

12:25.850 --> 12:32.290
So in our case this game over UI and go over to Inspector then overrides and then click on this apply

12:32.290 --> 12:32.690
all.

12:32.690 --> 12:37.370
So the changes you've made inside this game over UI game object it's going to be save.

12:37.370 --> 12:41.010
And let's also make this scene management game object a prefab.

12:41.050 --> 12:41.450
Why?

12:41.490 --> 12:43.810
Because we need this scene management script.

12:44.210 --> 12:44.970
So that's why.

12:45.010 --> 12:48.850
And the scene management script is sitting on the scene management game object.

12:48.850 --> 12:51.090
So that's why we're gonna make this one prefab.

12:51.090 --> 12:57.250
So we're gonna have the copy so we can use this scene management script to any scene that we want.

12:57.290 --> 13:02.770
So I'm going to select my project tab and let's select the Scene Management GameObject in the hierarchy.

13:02.930 --> 13:08.330
And let's drag it in inside this prefabs folder in order to turn it into a prefab.

13:08.530 --> 13:11.570
So as you can notice, we just turn it into a prefab.

13:11.610 --> 13:19.530
Basically, we have the copy of it by now, and we can use it into another level or another scene as

13:19.530 --> 13:19.930
well.
