‫Welcome back.
‫In this video, we will have a look at instantiating objects.
‫Therefore, let's go ahead and create a new 3D object.
‫I'm just going to use a cube here and I want to reproduce that cube.
‫Let's rotate it a little bit so we can see it better, maybe even add a material so we can see it even
‫better.
‫Or actually what we can do is change the background to solid color.
‫Now we can see our cube perfectly.
‫Okay, so what I want to do is I want to instantiate this object multiple times on my screen and I want
‫to do it via code, because so far what we did is we duplicated it and then it was there and we had
‫to move it around.
‫But we can do that via code as well.
‫So let's go ahead and do that.
‫Let's save that cube as a prefab and now let's create an empty object, which I'm going to call script.
‫I'm going to delete that cube.
‫And now within the script I want to have a script which I'm going to call instantiate cubes because
‫that's what it's going to do.
‫So very simple.
‫It's really just about the concept of instantiating objects.
‫And in order to instantiate something, you can instantiate it based on a prefab.
‫And what we need here is a prefab.
‫So a transform to be precise, transform prefab.
‫And within the start method I want to create multiple of those prefabs.
‫And how I do that is the following way for and I equals zero.
‫So I simply use a for loop in order to create them.
‫I lower ten I plus plus next we need to call the method instantiate clones the object original and returns
‫the clone.
‫So that's what it does.
‫It clones an object, for example, our prefab and then it instantiate it or it produces it at specific
‫positions.
‫We have multiple different overloads here.
‫I'm not going to go through them, but the one that we are going to use has a prefab at the beginning,
‫which is an object.
‫Then it needs a position where it's where it needs to be.
‫So I'm just going to use a vector three and I'm just going to use I multiply it with, let's say 3.0
‫F for the X position and for the Y position and Z position, I simply use zero.
‫Now, of course I need some more information here.
‫So the next parameter will be the criterion, and I'm just going to use Catalonian identity.
‫So there will be no additional rotation.
‫All right.
‫Now, in order to run the code, we, of course, need the prefab.
‫So let's drag our cube in here.
‫And now let's all right, now, let's run it.
‫And as we can see, of course, we have them all in order next to each other.
‫So they all have a different angle.
‫And of course they start at zero.
‫So what we could do is go ahead and change that.
‫All right, let's move it a little bit to the left.
‫So we simply use -15 plus this value that we have here and now.
‫We should see that we have them all in order.
‫And that we are.
‫So now we have our cubes all in order and you know how to create those cubes.
‫You could of course, do that via user input as well.
‫So if input dot get key down and here key code, I'm just going to use space.
‫And what I want to do here is I want to have a counter and counter is zero and I'm going to copy that
‫code here, delete the for loop and or delete the whole start method.
‫And now instead of I, I use my counter.
‫So now let's save that.
‫What that will do.
‫Each time that I use space, it will instantiate a new prefab at the position -15, plus whatever value
‫the counter has.
‫And of course we may not forget to increase the counter value, so let's increment that.
‫Now let's save the script and let's run it again.
‫And now let's press the space key several times to see what happens.
‫So I'm pressing the space key.
‫And as you can see, I'm zooming out a little bit and as you will see, it creates new cubes all the
‫time.
‫Perfect.
‫So now you know how to instantiate objects that will be very relevant for our next project.
‫So have fun with the project and see you in the next video.