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
On this page of the site you can watch the video online How to Spawn Random Objects in Unity with a duration of hours minute second in good quality, which was uploaded by the user Unity Ace 25 January 2022, share the link with friends and acquaintances, this video has already been watched 26,704 times on youtube and it was liked by 398 viewers. Enjoy your viewing!