How to Spawn Random Objects in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everyone! In this video we will learn  how to spawn random objects at random positions   inside Unity. By the end of the video we  would have created something like this.   Each time the Space key is pressed a random  object is spawned in our Unity scene.   I have a very simple scene setup in Unity which  only has a ground plane and I have created three   prefabs, a capsule, a cube and a sphere. All  of them have a Rigidbody attached to them   so they will have some gravity when we spawn  them. In the hierarchy I'll right click and   I'll create a new Empty Gameobject, I will rename  this object to Spawner and inside the Asset folder   i'll right click and i'll create a new c# script  and i'll rename the script to RandomObjectSpawner   Double click to open it in Visual Studio. Inside  Visual Studio the first thing i will do is I will   delete the start method because we won't be using  it and above the update method i will create a new   variable of the type Array, public Gameobject,  square brackets and I will give the name   myObjects. For those of you who don't know what  an array is, it's basically a container that   holds variables of the same type, more than one  variable of the same type in our case it will be   of the type Gameobject because the prefabs in  Unity are Gameobjects that we are planning on   spawning inside the update method. First of all i  will check if the player has pressed the space key   and if so we're gonna create two variables  so Input.GetKeyDown (Keycode.Space) The first variable I create will be of the type  integer and we're going to call it randomIndex   and this variable will store a random number  from 0 to the length of the array so int   randomIndex equals to Random.Range  from zero to myObject's.length   The next variable we'll create gonna be  of the type Vector3 we're gonna name it   randomSpawnPosition and each time the space key  is pressed a new random spawn position will be   created so we can use it in the instantiate method  We're gonna type Vector3 randomSpawnPosition   equals to new Vector3 for the x-coordinates we're  going to use the Random.Range from -10 to 11.   For the y coordinates we're gonna use the number  5 because we want the object to spawn above the   ground plane and for the z-axis we're going to do  the same as for the x-axis Random.Range from -10   to 11. Now we have everything set up to spawn  a random object at random positions and unity   i'm gonna use the Instantiate method so we're  gonna type Instantiate( for the object we want to   spawn we're gonna use the array variable myObjects  and for the index we're just gonna give the   randomIndex we have created for the position we're  going to use the randomSpawnPosition variable   as a second argument and for the rotation  we're just going to type Quaternion.Identity   which basically means no additional rotation.  Now save the script and go back into Unity.   Inside Unity I will select the Spawner object  and i will drag and drop the Script to it.   Now in the slot myObjects we're gonna  type the number of objects we have.   We had three objects so i'm gonna type three  and it gives us three slots for Game Objects   in our Array. I gonna lock the Inspector so I  can select and drag and drop the three objects.   The Capsule, the Cube and the  Sphere. Now if I go into Play Mode and press Space a new game object  is created at a random position.   Each time i press Space a random  object is spawned at a random position. Thank you for watching and see  you in the next one bye bye!
Info
Channel: Unity Ace
Views: 22,695
Rating: undefined out of 5
Keywords: unity, unity tutorial, unity3d, how to spawn random objects in unity, how to spawn random obstacles, unity spawn random prefabs, how to spawn random prefab in unity, unity random spawn, random object spawn unity, unity spawner, unity spawn, unity instantiate, unity random prefab spawn, unity spawn object at position, unity spawning objects, unity spawn enemies, spawning obstacles in unity, spawning random object in unity, unity spawner tutorial, unity spawn random objects
Id: bIM3VAiZHeQ
Channel Id: undefined
Length: 3min 54sec (234 seconds)
Published: Tue Jan 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.