How to spawn GAMEOBJECTS in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i will be answering the most ultimate question of all time how do we spawn game objects in unity others what are on my shirt hi and in this video we'll learn how to spawn game objects in unity engine i will start with an empty scene first let's create the object that we have to spawn so i'll go to the hierarchy right click 3d object cube you can have a look at the inspector for the cube i will then create a simple material and then apply it to the cube and now let's create a cd sharp script so we'll go to the project window right click create c sharp script and we will name it spawner and i'll double click to open it in visual studio once the script opens we'll get rid of the update function and let's start spawning so in order to spawn an object we use this method called instantiate as you can see here it belongs to the class object and returns an object in other words it clones the original object and returns the clone so at start we'll write instantiate followed by a couple of parentheses and inside these we are going to specify the object that we are going to instantiate if you look at the unity docs for instantiate function you will see that it contains five different overloads or signatures the ones i use quite often are the fourth and the fifth signatures the fourth signature allows us to specify a position which is a vector3 and a rotation which is a quaternion and the fifth one requires a transform which can act as a parent for the spawned instances next we need to create a reference for the game object that we need to spawn so we'll create an instance of type game object and name it object to be spawned this game object class inherits from the object class in order to make this field visible in the inspector we can either make this field public or you can use the serialized field attribute and keep it private and back over here we'll specify the object to be spawned and we'll go back into unity but we haven't assigned the script to anything so we'll create an empty game object and name it spawner and then drag and drop this script into the inspector and i'll press this dot here and then select the cube or you can just directly drag and drop this from the hierarchy if we play now you can see that there are two cubes in the hierarchy which means our spawning worked i'll just select this and move it away and now we have a cube which is a clone but now let's select the cube and move it a little bit and now if we play the game interesting now they are on top of one another so we can confirm that using the first signature we'll take in the transform of the original object into account so if we want to spawn it at a specific location let's say back at the center what you can do is to specify a position which is going to be a new vector 3 0 0 0 or you can replace it with vector3.0 which is exactly the same thing and we need to specify a quaternion for the rotation which is going to be quaternion dot identity which means zero rotation on our axis and now we have a cube at this location while the clone is spawned at the origin now we learn how to create a parent for the clones now we'll create an empty for the parent and name it cubes but let's take this step further we will create an integer to hold the number of items that we can spawn and at start we'll create a for loop and do the spawning inside let's give it a random position by using the random.range function and now we can specify the number of items let's set this to three we'll also create a serialized transform called parent and assign it as the fourth argument to set it as a parent so if you go back to spawner we can now specify the parent game object as the parent now if you hit play you can see that the three cubes will spawn and all of them will have a common parent if this doesn't work for some reason you can try using object.instantiate or gameobject.instantiate instead and that's how you do it you can now call this method anytime during the game and it will spawn any object now if you have multiple scenes and you have to spawn the same cube it's going to be difficult to go to each scene and then make changes to it if you need it but luckily for us we can create a prefab out of it and then spawn the prefab instead we can drag and drop the game object to the project to create a prefab we can now delete the original cube and now in the spawner we can drag and drop this prefab into the field instead you can see that the prefab gets instantiated notice how these things are called clones this happens because they are separate entities and they have no link back to their prefab their links are broken they have no recollection of one another let's just put a prefab in the scene you will notice that this is blue which means you can right click and perform prefab operations like selecting the prefab itself but with our spawned clones we cannot do that if you want that prefab connection for some reason it's very easy to get it let me just comment this out and then you can use the class prefab utility but it doesn't exist because you need to use the unity editor namespace and now you can write prefab utility dot instantiate prefab and specify the object to be spawned and you can specify a parent as well but this method has limited overloads compared to the original one and now if you play the game you can see that they are no longer clones and they are blue and now you can perform prefab operations by right clicking and prefab and then now you just select the asset i don't usually use this because it has limited overloads and in the end you'd have to use normal instantiate instead because this is only useful during development to be honest i don't use it at all but i'm just showing you because this may be something that you want to do i'll just comment this out that's it that's how we spawn a game object we have this instantiate method with a lot of overloads that we can use and we can call this at any time to spawn a game object you can obviously change the arguments of this method to your convenience so that's it for this video if you have some tips to share comment down below so that i can pin them and others may be able to read it more easily if you didn't like this video let me know what went wrong for you so that i can work on it if you have any suggestions on what i should do next let me know it would be really helpful so take care and see you in the next video i should have won a different shirt
Info
Channel: AnanDEV
Views: 1,005
Rating: undefined out of 5
Keywords: how to spawn gameobjects, instantiation in unity, spawning in unity, gameobject.instantiate, gameobject instantiate unity, prefabutility, spawning prefabs, unity instantiate, instantitate prefab, unity spawning objects, spawn gameobject unity, instantiate unity c#, unity c# spawn prefab, unity c# spawn gameobject, prefabutility.instantiateprefab
Id: ycPhF_UyFo4
Channel Id: undefined
Length: 6min 47sec (407 seconds)
Published: Tue Sep 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.