I am making a game where objects spawn at random locations and you need to shoot them, but I also want to make the objects spawn at random intervals of time, and I do not know how to do that.
This is the code that I am using:
public GameObject[] enemyPrefabs;
private int SpawnRangeZ = 47;
private float startDelay = 0.01f;
public float spawnInterval = 1.5f;
void Start()
{
InvokeRepeating("SpawnRandomEnemy", startDelay, spawnInterval);
}
void Update()
{
}
void SpawnRandomEnemy()
{
int enemyIndex = Random.Range(0, enemyPrefabs.Length);
Vector3 spawnPos = new Vector3(65, 0, Random.Range(-SpawnRangeZ, SpawnRangeZ));
Instantiate(enemyPrefabs[enemyIndex], spawnPos, enemyPrefabs[enemyIndex].transform.rotation);
}