How can I randomly pick from an array and spawn that object all the time. I want to pick one gameobject from array and spawn only it. I have 2 colors of gamobjects and I want to randomly pick one and instantiate only 1 randomly picked. My code:
public float spawnDelay;
public GameObject[] car;
public Transform spawnPoint;
public float nextTimeToSpawn = 0f;
void Update()
{
if (nextTimeToSpawn <= Time.time)
{
SpawnCar();
nextTimeToSpawn = Time.time + spawnDelay;
}
}
void SpawnCar()
{
int carIndex = Random.Range(0, car.Length);
GameObject randomCar = car[carIndex];
Instantiate(randomCar, spawnPoint.position, spawnPoint.rotation);
}