WEBVTT

00:00.040 --> 00:06.400
We're going to use for loop in order to spawn a three ground in the start of the game instead of spawning

00:06.400 --> 00:07.000
one.

00:07.000 --> 00:07.960
So let's do that.

00:07.960 --> 00:11.240
So we're going to select our Ground spawner GameObject in the hierarchy.

00:11.240 --> 00:15.360
And let's open up the ground spawner script by double clicking on it.

00:15.760 --> 00:20.080
So we as you can see we just uh, just calling this function.

00:20.240 --> 00:25.800
So, so what we're going to do, uh, we're going to use for loop in order to spawn uh, first three

00:25.800 --> 00:27.360
ground in the start of the game.

00:27.360 --> 00:30.480
Or you can just spawn as many ground you want.

00:30.720 --> 00:35.160
So what we will do, we will, uh, use for loop.

00:35.160 --> 00:40.440
So let's use this for and you can just, uh, tap twice on your keyboard.

00:40.440 --> 00:43.000
So basically the tap key double tap it.

00:43.280 --> 00:48.680
Uh firstly or you can say uh quickly you need to uh use this tap.

00:48.680 --> 00:49.800
So I'm going to press down.

00:49.800 --> 00:58.360
As you can see, as soon I press tap twice at the at once moment, we can see this for loop format just

00:58.360 --> 00:59.320
gets add up.

00:59.440 --> 01:05.770
And we can just do that or simply what you can do, you can just use this for and then make first bracket

01:05.770 --> 01:07.570
and then some curly brackets.

01:07.570 --> 01:12.290
And then you can see this something like I int I equal to zero.

01:13.090 --> 01:15.770
But it's pretty heavy to do.

01:15.770 --> 01:19.250
So we can simply going to use for uh tab key.

01:19.450 --> 01:22.610
So we're going to tap twice a tab key in our keyboard.

01:22.810 --> 01:24.730
So this for loop will run.

01:25.010 --> 01:29.490
And over this condition which is I is less than length.

01:29.610 --> 01:33.370
So instead of passing length we're going to pass something like uh three.

01:33.370 --> 01:35.130
So I want to spawn three ground.

01:35.130 --> 01:36.850
So that's why I just pass three.

01:36.970 --> 01:42.250
So this for loop will run until, until this condition over here breaks out.

01:42.250 --> 01:49.250
So it's gonna start a from I as you can see int I equal to zero means it's going to start from zero.

01:49.410 --> 01:53.770
So and let's suppose we uh we just set I is now zero.

01:53.770 --> 01:56.650
So we do know that zero is lesser than three.

01:56.650 --> 02:01.370
So the whatever you're gonna do inside this curly bracket, it's gonna be execute.

02:01.570 --> 02:05.410
And then in the next time this, I just going to add up one.

02:05.410 --> 02:07.770
So in this start this I is zero.

02:07.770 --> 02:10.850
So plus plus over here basically means adding one.

02:11.090 --> 02:13.170
So zero plus one is just going to be one.

02:13.170 --> 02:16.450
So the next time this for loop now I is one.

02:16.450 --> 02:18.850
And we do know that I is one.

02:18.850 --> 02:25.330
And one is still less than three I still less than three basically means the condition over here is

02:25.330 --> 02:26.170
still true.

02:26.170 --> 02:31.450
So that's why whatever you're going to do inside this curly bracket, it will execute for the second

02:31.450 --> 02:32.810
time as well.

02:32.970 --> 02:36.410
And now we're just adding one after doing this.

02:36.410 --> 02:37.650
So I is one.

02:37.650 --> 02:39.650
So one plus one is just going to be two.

02:39.810 --> 02:42.010
Now the I is two over here.

02:42.010 --> 02:44.050
So I is now two.

02:44.130 --> 02:46.890
And we do know that two is still less than three.

02:46.890 --> 02:49.610
So the condition over here is still a true.

02:49.610 --> 02:54.970
So whatever we're going to do inside this curly bracket, it's going to execute for the third time as

02:54.970 --> 02:55.330
well.

02:55.370 --> 02:58.370
Because the condition over here is totally true.

02:58.370 --> 03:01.970
So and after that, uh, I, I is now two.

03:02.010 --> 03:03.970
So two plus one is just going to be three.

03:03.970 --> 03:10.300
So the now the third time this I is now three and we do know that three is not less than three.

03:10.300 --> 03:11.340
So I is three.

03:11.700 --> 03:15.260
And we do know that three is not less than three.

03:15.260 --> 03:20.180
So whatever we're going to do inside this curly bracket now, it's not going to be executed.

03:20.180 --> 03:23.900
So because the condition over here is now totally false.

03:23.900 --> 03:30.060
So this for loop will run third time because the condition will be for the uh, till uh, the condition

03:30.060 --> 03:36.820
just going to be, uh, true, a true for third time, but in fourth time it's not going to be executed

03:36.820 --> 03:41.620
because in fourth time this condition is just going to break breaks out totally.

03:41.620 --> 03:42.460
So that's why.

03:42.620 --> 03:46.580
So this just gonna run this for loop, uh, third time.

03:46.580 --> 03:50.460
So what we're going to do in the third time, we're going to call this spawn crown.

03:50.460 --> 03:53.060
So we're going to press Ctrl X in order to cut.

03:53.060 --> 03:56.940
And we're going to paste inside this curly brackets by Ctrl V.

03:57.180 --> 04:02.540
So this spawn crown function just going to call third time because the this for loop is just going to

04:02.540 --> 04:04.100
run a third time.

04:04.300 --> 04:07.990
Uh, and the fourth time the condition over here is going to be false.

04:07.990 --> 04:13.230
So that's why the three grams is going to be spawned in the start of the game.

04:13.230 --> 04:18.590
Because inside this curly bracket, this spawn ground function is just going to run third time right

04:18.590 --> 04:18.990
away.

04:19.390 --> 04:21.630
Uh, basically this is one ground function.

04:21.630 --> 04:24.150
Just going to call uh, three times.

04:24.230 --> 04:27.470
Uh, first time, second time and third time and fourth time.

04:27.510 --> 04:30.350
The condition over just going to be false.

04:30.390 --> 04:35.670
Uh, so it's not going to run in the fourth time means, uh, the fourth time in the start of the game.

04:35.670 --> 04:39.830
We're not going to able to see any, uh, crown in the fourth time.

04:39.830 --> 04:45.350
So I'm going to make this a cut and let's press down Ctrl s in order to save our skip.

04:45.350 --> 04:47.110
And let's move over to unity.

04:47.150 --> 04:50.550
You will see in the start of the game we will have a look.

04:50.590 --> 04:55.990
The three grounds, uh, just gonna spawn right away right after the game start.

04:55.990 --> 04:58.750
And we're gonna have a look on that as well.

05:06.270 --> 05:08.430
So now we should see.

05:08.430 --> 05:12.630
So let's click on this play button in the top in order to take a look on this.

05:13.390 --> 05:18.550
And we will see that in the start of the game a three grams just gonna spawn.

05:24.270 --> 05:29.230
And we can see I'm going to click on this pause button in the top in order to pause my game.

05:29.590 --> 05:36.190
And we can see that, uh, three grams just gets spawned and the fourth crown spawn once we trigger

05:36.190 --> 05:42.670
it with this box Collider 2D, and we can see three ground just spawned in the start of the game one,

05:42.670 --> 05:43.830
two, and three.

05:44.110 --> 05:48.630
And this one will just get spawn because we trigger it with that box.

05:48.630 --> 05:52.910
Collider 2D, and we're just getting some, uh, error.

05:52.950 --> 05:54.310
We can ignore that.

05:54.310 --> 05:55.750
I hope you get the idea.

05:55.790 --> 06:03.030
Let's get out of play mode instead of spawning three, we can spawn as many ground we want in the start

06:03.030 --> 06:06.190
of the game, so you can just make a variable for that.

06:06.190 --> 06:11.920
Basically, to do that, move over to script and then go to top of your class and you can just make

06:11.960 --> 06:17.400
a public variable to just adjust it underneath your ground spawner script.

06:17.400 --> 06:20.840
So you can just make a variable in or how.

06:21.040 --> 06:22.280
In order to how many.

06:22.640 --> 06:23.120
Crowns.

06:23.120 --> 06:25.960
You just want to spawn in the start of the game.

06:25.960 --> 06:28.000
So I'm going to make a variable public.

06:28.240 --> 06:34.320
And we're going to make it public so we can adjust underneath our ground spawner script in Inspector.

06:34.320 --> 06:37.440
And that will be in like whole numbers 012, three, four.

06:37.440 --> 06:41.560
And we can call something like number of crowns.

06:41.600 --> 06:45.800
So we're going to call it number of rounds.

06:46.000 --> 06:51.760
And we will set equal to by default value I'm going to set something like three because I want three

06:51.800 --> 06:52.200
crowns.

06:52.200 --> 06:54.320
And we're going to close that up with semicolon.

06:54.320 --> 06:59.160
And over here instead of passing three we're going to pass this number of ground.

06:59.160 --> 07:01.720
So the number of ground by default we set three.

07:01.720 --> 07:02.520
So three ground.

07:02.560 --> 07:04.440
So this is going to be three right away.

07:04.440 --> 07:06.160
So I'm going to remove this three.

07:06.200 --> 07:10.760
Instead of hard coding we're going to pass the variable which is number of rounds.

07:10.960 --> 07:14.160
And we're going to press down Ctrl S in order to save our skip.

07:14.160 --> 07:15.960
And now let's move over to unity.

07:16.600 --> 07:19.880
And so this a number of grounds by default we set.

07:19.880 --> 07:25.320
So this just going to be three right away means the uh three grams just gonna spawn in the start of

07:25.320 --> 07:25.640
the game.

07:25.640 --> 07:31.600
And we do know we just made it public so we can, uh, adjust this number as many times we want or the

07:31.600 --> 07:33.880
number we would like to go with.

07:33.880 --> 07:35.400
So let's go to unity now.

07:35.400 --> 07:41.000
And we can see underneath our ground spawner script number of ground variable just gets pop up.

07:41.000 --> 07:43.080
And we can just click on this play button.

07:43.120 --> 07:48.360
And in order to check that and we're gonna able to see three ground just going to spawn right after

07:48.360 --> 07:49.680
the game start.

07:49.680 --> 07:51.520
But we can adjust this number.

07:51.520 --> 07:53.440
So that's why we just made it public.

07:53.600 --> 07:55.760
So we uh we can do it.

07:55.800 --> 07:59.400
As you can see in the start of the game, three ground just gets spawn.

07:59.440 --> 08:00.880
Let's get out of play mode.

08:00.880 --> 08:08.400
And you can just choose this number of ground as well by over here as many ground you want in the start

08:08.400 --> 08:08.960
of the game.

08:08.960 --> 08:10.320
It's totally up to you.

08:10.320 --> 08:15.680
I hope you get the idea that how you can use for loop to do specific things.
