How to Spawn Objects in Unity [Using Instantiate]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we will learn the basic corresponding objects in unity we will use the instantiate method which is built in method by unity at the end of the video we'll have something like this when you press the space key a new cube will respond okay let's begin i have a simple scene setup with a ground plane and a cube object i will select the cube object and i will add rigid body to it after that stand i will drag and drop the cube in our asset folder this will create a prefab out of it i'll delete the cube in the scene up in the hierarchy panel i will right click and i'll create a new empty object i'll rename this object to spawner i'll reset its transform and i'll move it up the y-axis now this will be the position that we spawn our keypad down in the set folders i will right click and i will create a new csub script i'll rename the script to spawner script double click to open it in visual studio okay visual studio first of all i'll delete the start method because i won't be using it and i'll create a new variable public game object and i will call it cube prefab this variable will hold our cube prefab that we just made in the unity scene in the update method i'll create a function to check if the player has pressed the sp the spacebar so if input that gate key down key code space then i will create or instantiate a new game object in our scene now first of all inside instantiate method we're going to give it three parameters first will be the object we want to spawn so cube prefab the second argument will be the position we want the object to be spawned so transform that position now this will be the position that the spawner object we have just created in the scene earlier and the third parameter we need to give it is the rotation we're gonna type quaternion that identity this basically means that it will have no rotation return on that identity if i hover over the instantiate method we're going to see that it takes a game object second parameter it takes is a vector3 position and the third parameter it takes is quaternion.rotation so dot rotation is basically no rotation after i'm done i will save the c subscript and go back into unity okay i will click on the spawner object and now drag and drop the script we have just made inside the cube prefab i will drag and drop the cube prefab we have just made now if i go into play mode and press on the space button a new cube is spawned and another one and another one i can press space as much times as i want and each time a new cube will be spawned that's all for this video in the next video we'll learn how to spawn objects at random location in our scene thank you for watching and see you in the next one bye
Info
Channel: Unity Ace
Views: 40,923
Rating: undefined out of 5
Keywords: unity, unity3d, game development, how to spawn gameobject in unity, unity spawning objects, unity spawning enemies, unity instantiate prefab, how to instantiate an object in unity, spawning objects unity 3d, unity instantiate, spawn prefabs unity, how to spawn objects in unity 3d, instantiate object unity, spawning objects in unity, spawn in unity, instantiate in unity, unity spawn object at position, unity spawner, unity spawn, unity spawn enemies, unity3d spawn prefab
Id: aBzpvUXibw0
Channel Id: undefined
Length: 3min 41sec (221 seconds)
Published: Sun Jan 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.