How to Spawn Random Objects in Unity
In this video of the Spawning Series we will learn How to Spawn Random Objects in Unity. We will use an array to store three GameObjects and in play mode we will pick a Random Object to be Spawned by pressing the Space key. Now please note that I use the Space key to Spawn Random Objects for demonstration purposes. You can Spawn them using a timer or trigger etc. If this video helped you please leave a like and Subscribe so I can make more of them. Cheers! CODE USED IN THE VIDEO: using System.Collections; using System.Collections.Generic; using UnityEngine; public class RandomObjectSpawner : MonoBehaviour { public GameObject[] myObjects; void Update() { if (Input.GetKeyDown(KeyCode.Space)) { int randomIndex = Random.Range(0, myObjects.Length); Vector3 randomSpawnPosition = new Vector3(Random.Range(-10, 11), 5, Random.Range(-10, 11)); Instantiate(myObjects[randomIndex], randomSpawnPosition, Quaternion.identity); } } } #SpawnRandomObject #UnitySpawnRandom #UnityRandomSpawning
Download
0 formatsNo download links available.