As you can see, I have created multiple crowns.
So if I move over to my project tab, then assets and then prefabs, I just created multiple and we
can just double click on it in order to open it up in prefab mode.
So I'm going to double click on this something like this Crown underscore nine.
As you can see, as soon as we open it up we can see our crown.
So I have done the same thing.
I just make this box collider as the size of this crown, and we just assign it this ground or this
parent game object as a box tag, as you can see in Inspector.
So if I select this crown underscore nine GameObject in the hierarchy, then move over to Inspector.
You can see tag.
We just tag it as a box tag.
So make sure you just tag it with something else as we have done already.
So we're going to go back.
So in hierarchy you're going to see a back arrow.
So just click there in order to move over to scene view or just to go back.
And so what we're going to do now we're going to create a script in order to spawn.
So I'm going to delete I'm going to delete this ground underscore nine sorry ground underscore one game
object or this ground underscore one I'm going to delete.
And I'm going to drag it something else something.
Or you can say uh other ground.
So I'm going to select my ground underscore one GameObject in the hierarchy.
And I'm going to delete that.
And I'm going to select a perfect ground for from the starting of the game.
So that's why I'm going to select this one.
So this one is ground underscore six.
So I'm going to select that drag it over to this hierarchy or into this scene view.
So I'm going to drag it on to on top of this ground underscore C sorry uh on in this hierarchy.
And we are ready to uh, you can simply say we are ready to spawn.
But before we do, let's have a look on this.
So I'm going to click on this play button in the top in order to play our game.
And you will see that.
So we need to spawn once we just trigger it or that box, then we're going to spawn the next round from
a next point.
So as you can see, we are moving, but we moving too much slowly.
So I'm going to make the speed a little bit faster.
But before we do, let's get out of play mode and let's create a script in order to spawn the next round.
So let's do that.
So we're going to move over to this project tab, then assets.
And then we're going to move over to this scripts folder.
So right click there.
Or you can simply right click on this assets as well.
And then create in the top.
And you're going to see Monobehaviour script.
Or you can just move over to this scripting and select this Monobehaviour script because we're going
to attach this script to our game object.
So that's why we need to create a Monobehaviour script.
So I already have created over here.
As you can see I have a script called Ground Spawner.
So what we need to do now, we need to attach this script to any game object or in hierarchy, or we
can simply create a separate game object for this script.
So let's do that.
So I'm going to move over to hierarchy.
Right click Hierarchy in Hierarchy or click on this plus icon.
Then create an empty game object.
And I'm going to name this game object to something like as the name of the script.
So I'm going to name it something Crown spawner and make sure make sure you just go to transform transform
in the inspector right click and reset it.
And now we're going to select the script from that folder and drag it over to this Add Component section
in this ground spawner game object in the Inspector.
So basically you need to attach.
Or you can simply click on this add component.
And you can find the script by script by typing name of the script.
So in our case ground spawner.
So as soon as we type it's just pop up.
And I'm not going to click on that in order to attach it twice.
So I already have attached.
So I'm not gonna do it again.
So let's open up our script by double clicking on it.
And what we're going to do?
We're going to remove this update function.
And also this comment you can say.
So we're going to remove that to make our skip a clean.
And we're going to make a function in order to spawn that.
So we're going to make it somewhere over here.
And we're going to make this function public because we're going to call this function through another
class.
Or you can simply say through another script.
So that's why we're going to make it public.
So make sure you just making it public.
And for function we need to type void.
And we have to name the function what we want to name it.
So we're going to call it something like spawn and make spawn ground.
Or we can call or we can simply call it spawn.
But I'm going to call it spawn ground and then make some curly brackets and that's it.
And we are ready to spawn.
So what we need we need we need a reference to our ground game object.
So if we move over to unity and then move over to this project app assets and then prefabs, as you
can see we have many grounds.
So we're going to take a random one.
And then we're going to spawn it.
So we need a reference of all of these a game object.
Or you can simply say all of this ground.
And we do know this ground itself, whole a game object.
So we basically need a reference of all of these ground GameObject prefab.
So let's do that.
So what we're going to do now we're going to move over to top of our class.
And we're going to grab the reference first.
Basically we need all of this ground in order to choose a random one.
And then we're going to spawn that.
So let's go to top of our class and we're going to make it public a public so we can adjust through
underneath our inspector.
And the game object we're looking for is a ground prefab.
Game object basically means all of those itself a game object.
So that's why we need to type this game object.
So the ground game object itself a game object.
So that's why we need to type this variable type which is GameObject.
And we have more than one.
So if we move to unity we can see we have ground one ground two.
And all of these are a little bit different.
So we need all of them.
And then we're going to take a random one and then we will spawn it.
So we need all of them.
So that's why we're going to make it array.
So we need to make this square brackets.
So square brackets basically just referencing it's an array array.
Basically it's gonna hold up multiple things.
It can be numbers.
It can be game object.
It can be position anything like that.
So we can see we have multiple grounds.
So that's why we just created a game object.
And then we just made it array because we're going to put all of this ground this ground one.
Round two.
Round three.
And and so on.
We're going to take all of them and we're going to put it into this array.
Or you can see this is a container which can hold multiple values or multiple ground.
And we have to name the array what we want to name it.
So I'm going to call it something like uh grounds.
So let's call it grounds sorry grounds.
And let's close that up with semicolon.
And also we're going to make a index parameter in order to keep track of which is a crown we just want
to spawn.
So let's suppose we just want to spawn the first one.
Then we're going to create a index parameter in order to keep which ground we want to spawn.
So we're going to take a look on that later.
So let's press down Ctrl S in order to save our script and then move over to unity.
So underneath your ground spawner script you're going to see an array just going to pop up.
And we just named that array is ground.
So what we need to do there we need to select all of this ground.
This ground underscore one ground underscore two ground underscore three and so on.
We need to select all of them.
And we need to put it into that array which is the ground.
So let's select our ground spawner GameObject in our hierarchy.
And we can see underneath our ground spawner script in Inspector a ground array just pop up and it says
list is empty.
Basically means this array is totally empty.
So what we have to do now, we need to select this ground one by one.
And we can drag it over here.
But instead of doing that what you can do, you can see in Inspector A top right, a lock button.
So just lock it so you can just, uh, select all of this ground and you can drag it at one time.
So what we're going to do now, we're going to select all of this ground by holding down shift.
Or you can simply press down.
You can hold down Ctrl and you can select those ground and drag it over to this grounds array in this
ground uh under ground spawner script.
As you can see now we can see we have all of this ground.
We have ground underscore one ground underscore two ground underscore three basically means we have
all of this ground.
And now we are ready to spawn them.
So make also you make sure you just go to Inspector one more time and then click on this lock button
in the top right to unlock the inspector.
Now we just unlocked our inspector by now.
So the first ground as you can see it just get defined or assign it with a some certain index so we
can see the ground underscore one.
Basically this ground is index is zero, and the second ground, which is the ground underscore two
is index is one.
As you can see it says element one.
Basically it just gets defined with an index.
And let's suppose we just want to spawn the fifth ground.
Basically if we basically this one which is the fifth one.
So if we just want to spawn we need to take a look on the index of it.
So the fifth we can say ground underscore five means the fifth ground.
And the index we can see is four.
So we can simply pass the index in order to spawn that.
So all of these are the ground just gets assigned with some certain index.
And we can see underneath our ground spawner script or this underneath our grounds array.
And now we are ready to uh, basically we are ready to spawn that.
So let's open up our script by double clicking on it.
So let's double click on that.
And what we're going to do we need to we need to move over to this spawn ground function.
But before we do let's create an int parameter.
So let's go to the top of our class.
And it doesn't need to be need to be public.
Instead we're gonna make it private.
So we're not gonna be able to see this parameter underneath our ground spawner script.
And that will be int basically whole numbers like zero, one, two, three, four.
And we're going to call it something like index prefab.
Or we can simply call it prefab index or let's call it ground prefab index.
Or I'm going to call it index shortly.
So I'm going to call it index.
And let's close that off with semicolon.
So why I created this parameter.
Basically this parameter over here index is just going to keep track of which uh ground we just want
to spawn.
So we're just gonna keep gonna keep track of the index of that ground.
So let's suppose we just want to spawn the first ground.
And we can see underneath our ground spawner script, the first ground index we can see zero.
So that's why we just created this index parameter.
In order to keep track of which ground we simply want to spawn.
And now we're going to move over to this start function.
And we do know that start function get call in the start of the game.
But let's take a look on this later.
What we're going to do now we also need a second parameter.
Basically we just want to spawn our ground.
And so what we need we need to spawn the next ground from this point.
Or you can simply call a if we select our ground underscore six game object in my hierarchy, we can
see we just created a next position.
So we created this position in order to just spawn the next ground from this position.
So we also need a second parameter which will be position or basically where we just want to spawn our
next ground.
So let's suppose we just want to spawn the next round from somewhere over here.
So we also need to define the position.
Then we can spawn the next ground from that position.
So that's why we will go to top of our class.
And we're going to make also a second parameter.
And it will be position.
And we're going to make it public.
So we can just simply, uh, simply basically, uh, adjust it underneath our this ground spawner script
so we can something over here and that will be vector to a vector to which has x axis, y axis.
No.
Basically it's gonna define our position.
So we're gonna have this x axis basically means horizontal as well as well as the vertical which is
the y axis.
So that's why we just created this vector two parameter.
Basically this just going to reference our uh represent our position.
And we can simply call it uh.
Next is spawn post.
So we're going to call it next.
And next spawn our spawn post.
Or simply you can simply call it something like next spawn position.
And we're going to close that up with semicolon.
And now what we're going to do we're going to move over to this spawn ground function.
And in this function what we just want to do, we just want to spawn.
And in order to spawn something we need to use this instantiate function and this instantiate function.
So make sure you just make these brackets and this instantiate function as a first argument.
It's going to take the game object you want to spawn.
So in our case we're gonna spawn this, uh, prefabs of this ground, as you can see in the unity editor.
So we just want to spawn, uh, or just want to choose a random one, then we're gonna spawn that.
So, uh, and we have it over here, as you can see, our top of our player, Skip.
And the second thing, this instantiate function is just going to take the position from where you want
to spawn.
So we also have a position parameter as you can see.
So we're going to spawn the next round from this position.
And the third argument it's going to take the rotation.
So we're not gonna rotate like that or we're not going to play with rotation.
So we're gonna simply pass quaternion.
Dot identity basically means no rotation.
So let's pass the variable.
So first thing it's going to take our uh the A you can simply say uh simply say the uh which ground
you basically want to spawn or which thing you want to spawn.
So in our case, we're gonna spawn ground.
So we need to pass the array name.
So which is in our case this ground.
And then you need to make this square brackets and this square brackets.
In between these square brackets you basically need to pass the index.
So let's suppose we want to spawn the first ground.
So basically this one if I just double click on it.
So the this ground.
So if you just want to spawn that that then you need to pass the index of that ground.
So we can go back and we can see the index index of that ground underneath our ground spawner script.
So the first ground uh underscore one game object or the first ground index we can see zero.
And the second ground index is two.
Sorry.
The second ground index is one.
As you can see I'm really apologize for that.
And let's suppose you just want to spawn the seventh ground.
So we can see seventh ground index is six.
So we need to pass the index uh, or uh, which index?
We basically want to, uh, basically want to, uh, spawn.
So let's suppose I just want to spawn this ground, uh, which is the ground underscore six.
So we need to see the index of that ground.
And so the six ground index is five as you can see.
So we can simply pass the index of it.
So I'm going to pass over here five and comma.
So make sure you just you need to pass the uh array first array name first.
So in our case grounds as you can see.
And then you need to make this square brackets in order to specify the index.
So I just pass five five basically means the sixth round.
And the second thing it's going to take the position.
So we're going to pass this next spawn post.
And we're going to define the position in the x and y somewhere something like over here.
So we're going to take a look later.
So for now you can simply pass the position.
So we're going to pass our next spawn post.
And then comma it's going to take the rotation.
So we're not gonna rotate like that.
So we're going to pass simply quaternion dot identity basically means no rotation.
And make sure you just close that up with semicolon.
So we also need to call this as spawn ground function.
But when we should call it once we trigger with this uh ground.
But for now let's call it in the start function.
And we do know that this start function get call in the start of the game.
So in the start of the game we just want to spawn our next ground from somewhere over here.
So we're going to call this uh this spawn ground function in order to spawn this ground, which in our
case the sixth ground and the index, uh, it, the index of it, uh, five, as you can see.
So basically, we're gonna spawn the sixth ground, uh, from this position with no rotation, as you
can see.
So let's call it in this start function.
So let's call start in start function which is spawn ground and make first bracket.
And then close that up with semicolon.
And we're going to press down Ctrl control is in order to save our script.
And let's move over to unity.
So you will see that once we start our game.
So if we just click on this play button in the top in order to play our game, you will see right after
the game start the that uh, the sixth round just gonna spawn uh, in the start of the game.
So we're gonna spawn it something over here.
So what we can simply do, we can note the position.
So in order to do that I'm going to create an empty game object.
So let's move over to this hierarchy.
Right click or hit this plus icon.
Then create an empty game object.
And we're going to select the move tool from over here in this in view.
And we're going to move it something over here.
And we can note the position.
So you can just simply select the game object first and then move over to Inspector then transform.
And you can note the position in the X.
So we can see in the x 55 and in the y which is uh -2.25.
Or we can simply put zero as well.
So it's going to be somewhere around here so we can see the position over here in the X is 55.
So we're going to do 55 something like that.
Or we can simply do I guess 55 point.
Let's do 55.5.
So 55.5in the x and in the Y zero.
And we can just ignore the z axis so we can ignore that.
So now what we're going to do we're going to select our ground spawner game object.
And we can see underneath our ground spawner script in the Inspector index spawn just gets a pop up
and it says x to zero and y to zero.
So by default we just didn't set any default value.
So that's why in the X it just became zero.
And in the Y it just gonna be zero.
So basically means this position.
And we know that this position in the x is zero and the y is zero.
So we don't want to spawn it from over here.
And we just want to spawn that from here.
So we have seen the position or we just noted the position in the X which is 55.5in.
The x axis.
So that's why in the x we're going to put something like 55.5.
And in the Y we're going to leave it to zero.
So it's going to be.
So the position is just going to be over here.
So the next spawn post we just define it in the X which is 55.5.
And in the Y zero basically means somewhere over here.
So this grounds five basically means the sixth round.
Just gonna spawn next spawn post.
And we just define it in the X which is 55 5.5.
And in the y zero basically means the over here, something over here.
And with no rotation we can see we just are spawning.
So let's go to unity now and let's have a look.
So the next or so basically the sixth gram should be spawn from over here.
So let's have a look on that.
So I'm going to click on this play button.
And we do know in the start of the game we just calling that function which is spawn and spawn ground
function.
Just doing the best thing.
It's gonna spawn that sixth gram from that position as we just passed.
So as you can see, as soon as the game started, we can see this as the six.
I'm going to click on this pause button in the top in order to pause my game.
And we can see in our hierarchy ground underscore six clone basically means the prefab just gets spawn.
And if we select that move over to Inspector.
We can see in the X which is 55.5, as we said.
And the y you can see is zero.
So basically this position.
So this six ground adjust gets spawned from this position.
And you can just pass any position that you would like to go with.
So I just want to spawn it from here.
So that's why I just simply pass in the X which is 55.5 means from afar from our main camera.
So this position in the X, which is 55.5 and in the Y zero.
So that's why the six ground just gets spawn from this position.
So I'm going to get out of play mode.
I hope you get the idea that how you can spawn it as that, how you can spawn the next ground.