We're going to use for loop in order to spawn a three ground in the start of the game instead of spawning
one.
So let's do that.
So we're going to select our Ground spawner GameObject in the hierarchy.
And let's open up the ground spawner script by double clicking on it.
So we as you can see we just uh, just calling this function.
So, so what we're going to do, uh, we're going to use for loop in order to spawn uh, first three
ground in the start of the game.
Or you can just spawn as many ground you want.
So what we will do, we will, uh, use for loop.
So let's use this for and you can just, uh, tap twice on your keyboard.
So basically the tap key double tap it.
Uh firstly or you can say uh quickly you need to uh use this tap.
So I'm going to press down.
As you can see, as soon I press tap twice at the at once moment, we can see this for loop format just
gets add up.
And we can just do that or simply what you can do, you can just use this for and then make first bracket
and then some curly brackets.
And then you can see this something like I int I equal to zero.
But it's pretty heavy to do.
So we can simply going to use for uh tab key.
So we're going to tap twice a tab key in our keyboard.
So this for loop will run.
And over this condition which is I is less than length.
So instead of passing length we're going to pass something like uh three.
So I want to spawn three ground.
So that's why I just pass three.
So this for loop will run until, until this condition over here breaks out.
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.
So and let's suppose we uh we just set I is now zero.
So we do know that zero is lesser than three.
So the whatever you're gonna do inside this curly bracket, it's gonna be execute.
And then in the next time this, I just going to add up one.
So in this start this I is zero.
So plus plus over here basically means adding one.
So zero plus one is just going to be one.
So the next time this for loop now I is one.
And we do know that I is one.
And one is still less than three I still less than three basically means the condition over here is
still true.
So that's why whatever you're going to do inside this curly bracket, it will execute for the second
time as well.
And now we're just adding one after doing this.
So I is one.
So one plus one is just going to be two.
Now the I is two over here.
So I is now two.
And we do know that two is still less than three.
So the condition over here is still a true.
So whatever we're going to do inside this curly bracket, it's going to execute for the third time as
well.
Because the condition over here is totally true.
So and after that, uh, I, I is now two.
So two plus one is just going to be three.
So the now the third time this I is now three and we do know that three is not less than three.
So I is three.
And we do know that three is not less than three.
So whatever we're going to do inside this curly bracket now, it's not going to be executed.
So because the condition over here is now totally false.
So this for loop will run third time because the condition will be for the uh, till uh, the condition
just going to be, uh, true, a true for third time, but in fourth time it's not going to be executed
because in fourth time this condition is just going to break breaks out totally.
So that's why.
So this just gonna run this for loop, uh, third time.
So what we're going to do in the third time, we're going to call this spawn crown.
So we're going to press Ctrl X in order to cut.
And we're going to paste inside this curly brackets by Ctrl V.
So this spawn crown function just going to call third time because the this for loop is just going to
run a third time.
Uh, and the fourth time the condition over here is going to be false.
So that's why the three grams is going to be spawned in the start of the game.
Because inside this curly bracket, this spawn ground function is just going to run third time right
away.
Uh, basically this is one ground function.
Just going to call uh, three times.
Uh, first time, second time and third time and fourth time.
The condition over just going to be false.
Uh, so it's not going to run in the fourth time means, uh, the fourth time in the start of the game.
We're not going to able to see any, uh, crown in the fourth time.
So I'm going to make this a cut and let's press down Ctrl s in order to save our skip.
And let's move over to unity.
You will see in the start of the game we will have a look.
The three grounds, uh, just gonna spawn right away right after the game start.
And we're gonna have a look on that as well.
So now we should see.
So let's click on this play button in the top in order to take a look on this.
And we will see that in the start of the game a three grams just gonna spawn.
And we can see I'm going to click on this pause button in the top in order to pause my game.
And we can see that, uh, three grams just gets spawned and the fourth crown spawn once we trigger
it with this box Collider 2D, and we can see three ground just spawned in the start of the game one,
two, and three.
And this one will just get spawn because we trigger it with that box.
Collider 2D, and we're just getting some, uh, error.
We can ignore that.
I hope you get the idea.
Let's get out of play mode instead of spawning three, we can spawn as many ground we want in the start
of the game, so you can just make a variable for that.
Basically, to do that, move over to script and then go to top of your class and you can just make
a public variable to just adjust it underneath your ground spawner script.
So you can just make a variable in or how.
In order to how many.
Crowns.
You just want to spawn in the start of the game.
So I'm going to make a variable public.
And we're going to make it public so we can adjust underneath our ground spawner script in Inspector.
And that will be in like whole numbers 012, three, four.
And we can call something like number of crowns.
So we're going to call it number of rounds.
And we will set equal to by default value I'm going to set something like three because I want three
crowns.
And we're going to close that up with semicolon.
And over here instead of passing three we're going to pass this number of ground.
So the number of ground by default we set three.
So three ground.
So this is going to be three right away.
So I'm going to remove this three.
Instead of hard coding we're going to pass the variable which is number of rounds.
And we're going to press down Ctrl S in order to save our skip.
And now let's move over to unity.
And so this a number of grounds by default we set.
So this just going to be three right away means the uh three grams just gonna spawn in the start of
the game.
And we do know we just made it public so we can, uh, adjust this number as many times we want or the
number we would like to go with.
So let's go to unity now.
And we can see underneath our ground spawner script number of ground variable just gets pop up.
And we can just click on this play button.
And in order to check that and we're gonna able to see three ground just going to spawn right after
the game start.
But we can adjust this number.
So that's why we just made it public.
So we uh we can do it.
As you can see in the start of the game, three ground just gets spawn.
Let's get out of play mode.
And you can just choose this number of ground as well by over here as many ground you want in the start
of the game.
It's totally up to you.
I hope you get the idea that how you can use for loop to do specific things.