1 00:00:03,990 --> 00:00:04,770 ‫Welcome back. 2 00:00:04,800 --> 00:00:08,630 ‫In this video, we will have a look at instantiating objects. 3 00:00:08,640 --> 00:00:13,530 ‫Therefore, let's go ahead and create a new 3D object. 4 00:00:13,530 --> 00:00:19,080 ‫I'm just going to use a cube here and I want to reproduce that cube. 5 00:00:19,080 --> 00:00:25,170 ‫Let's rotate it a little bit so we can see it better, maybe even add a material so we can see it even 6 00:00:25,170 --> 00:00:25,830 ‫better. 7 00:00:26,220 --> 00:00:30,930 ‫Or actually what we can do is change the background to solid color. 8 00:00:31,320 --> 00:00:33,360 ‫Now we can see our cube perfectly. 9 00:00:33,540 --> 00:00:41,280 ‫Okay, so what I want to do is I want to instantiate this object multiple times on my screen and I want 10 00:00:41,280 --> 00:00:47,160 ‫to do it via code, because so far what we did is we duplicated it and then it was there and we had 11 00:00:47,160 --> 00:00:48,210 ‫to move it around. 12 00:00:48,210 --> 00:00:50,460 ‫But we can do that via code as well. 13 00:00:50,460 --> 00:00:52,350 ‫So let's go ahead and do that. 14 00:00:52,470 --> 00:01:01,230 ‫Let's save that cube as a prefab and now let's create an empty object, which I'm going to call script. 15 00:01:02,970 --> 00:01:04,590 ‫I'm going to delete that cube. 16 00:01:05,670 --> 00:01:16,080 ‫And now within the script I want to have a script which I'm going to call instantiate cubes because 17 00:01:16,080 --> 00:01:17,400 ‫that's what it's going to do. 18 00:01:17,760 --> 00:01:19,020 ‫So very simple. 19 00:01:19,020 --> 00:01:22,410 ‫It's really just about the concept of instantiating objects. 20 00:01:22,410 --> 00:01:28,230 ‫And in order to instantiate something, you can instantiate it based on a prefab. 21 00:01:28,500 --> 00:01:32,940 ‫And what we need here is a prefab. 22 00:01:33,390 --> 00:01:38,250 ‫So a transform to be precise, transform prefab. 23 00:01:39,690 --> 00:01:44,580 ‫And within the start method I want to create multiple of those prefabs. 24 00:01:44,580 --> 00:01:52,050 ‫And how I do that is the following way for and I equals zero. 25 00:01:52,050 --> 00:01:55,260 ‫So I simply use a for loop in order to create them. 26 00:01:55,830 --> 00:02:07,560 ‫I lower ten I plus plus next we need to call the method instantiate clones the object original and returns 27 00:02:07,560 --> 00:02:08,160 ‫the clone. 28 00:02:08,160 --> 00:02:09,300 ‫So that's what it does. 29 00:02:09,300 --> 00:02:16,980 ‫It clones an object, for example, our prefab and then it instantiate it or it produces it at specific 30 00:02:16,980 --> 00:02:17,850 ‫positions. 31 00:02:17,850 --> 00:02:20,160 ‫We have multiple different overloads here. 32 00:02:20,160 --> 00:02:25,200 ‫I'm not going to go through them, but the one that we are going to use has a prefab at the beginning, 33 00:02:25,200 --> 00:02:26,340 ‫which is an object. 34 00:02:27,150 --> 00:02:31,890 ‫Then it needs a position where it's where it needs to be. 35 00:02:31,890 --> 00:02:42,060 ‫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 36 00:02:42,060 --> 00:02:50,280 ‫F for the X position and for the Y position and Z position, I simply use zero. 37 00:02:50,280 --> 00:02:53,040 ‫Now, of course I need some more information here. 38 00:02:53,040 --> 00:02:59,190 ‫So the next parameter will be the criterion, and I'm just going to use Catalonian identity. 39 00:02:59,400 --> 00:03:02,100 ‫So there will be no additional rotation. 40 00:03:03,480 --> 00:03:03,810 ‫All right. 41 00:03:03,810 --> 00:03:06,510 ‫Now, in order to run the code, we, of course, need the prefab. 42 00:03:06,510 --> 00:03:09,510 ‫So let's drag our cube in here. 43 00:03:09,660 --> 00:03:11,760 ‫And now let's all right, now, let's run it. 44 00:03:11,760 --> 00:03:16,530 ‫And as we can see, of course, we have them all in order next to each other. 45 00:03:16,530 --> 00:03:18,090 ‫So they all have a different angle. 46 00:03:18,090 --> 00:03:20,160 ‫And of course they start at zero. 47 00:03:20,160 --> 00:03:23,700 ‫So what we could do is go ahead and change that. 48 00:03:23,700 --> 00:03:25,410 ‫All right, let's move it a little bit to the left. 49 00:03:25,410 --> 00:03:32,370 ‫So we simply use -15 plus this value that we have here and now. 50 00:03:32,370 --> 00:03:35,250 ‫We should see that we have them all in order. 51 00:03:37,250 --> 00:03:37,900 ‫And that we are. 52 00:03:37,910 --> 00:03:43,550 ‫So now we have our cubes all in order and you know how to create those cubes. 53 00:03:43,580 --> 00:03:46,610 ‫You could of course, do that via user input as well. 54 00:03:46,610 --> 00:03:56,660 ‫So if input dot get key down and here key code, I'm just going to use space. 55 00:03:58,130 --> 00:04:08,390 ‫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 56 00:04:08,390 --> 00:04:13,840 ‫code here, delete the for loop and or delete the whole start method. 57 00:04:13,850 --> 00:04:16,820 ‫And now instead of I, I use my counter. 58 00:04:18,620 --> 00:04:19,720 ‫So now let's save that. 59 00:04:19,730 --> 00:04:20,570 ‫What that will do. 60 00:04:20,570 --> 00:04:28,010 ‫Each time that I use space, it will instantiate a new prefab at the position -15, plus whatever value 61 00:04:28,010 --> 00:04:28,760 ‫the counter has. 62 00:04:28,760 --> 00:04:33,410 ‫And of course we may not forget to increase the counter value, so let's increment that. 63 00:04:33,680 --> 00:04:36,410 ‫Now let's save the script and let's run it again. 64 00:04:36,410 --> 00:04:40,520 ‫And now let's press the space key several times to see what happens. 65 00:04:40,520 --> 00:04:42,740 ‫So I'm pressing the space key. 66 00:04:42,740 --> 00:04:50,210 ‫And as you can see, I'm zooming out a little bit and as you will see, it creates new cubes all the 67 00:04:50,210 --> 00:04:50,750 ‫time. 68 00:04:50,750 --> 00:04:51,500 ‫Perfect. 69 00:04:51,980 --> 00:04:58,270 ‫So now you know how to instantiate objects that will be very relevant for our next project. 70 00:04:58,280 --> 00:05:02,360 ‫So have fun with the project and see you in the next video.