smokereaper98
Member
- Joined
- Nov 16, 2019
- Messages
- 12
- Programming Experience
- 1-3
I have a empty objects that is a spawner on my scene and i want it to spawn a random prefab every an x amount of time.. i use this code but i cant figure it out.. im sure there is an easyer way to create a random call from an array.
C#:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class obstacleSpawner : MonoBehaviour
{
//vars
public Rigidbody rb;
public float ForwardForce;
public GameObject Obstacle2;
public GameObject Obstacle1;
private Vector3 test;
//lists
public List<string> obstacles = new List<string>();
void Start()
{
obstacles.Add("Obstacle1");
obstacles.Add("Obstacle2");
InvokeRepeating("spawnObstacle", 3f, 3f);
}
void Update()
{
rb.velocity = new Vector3(rb.velocity.y, rb.velocity.y, ForwardForce);
}
void spawnObstacle()
{
System.Random r = new System.Random();
int genRand = r.Next(1, 2);
test = new Vector3(10.47f, 0.4096543f, transform.position.z);
Instantiate(obstacles[r], test, transform.rotation);
}