How would I implement a random spawn timer into a list based spawn system?

Currently I’m spawning ships to these transforms, which move at random speeds on a single axis and destroy after a set time. What I need is to implement a random spawn timer into the following script to create the illusion of traffic flow, which can be edited in the inspector for performance/density balance.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Ship_Traffic_Spawner : MonoBehaviour
{
	public int numberOfItemsToSpawn = 49; // modifiable in the inspector
	public List<Transform> spawnLocations = new List<Transform>();
	public List<GameObject> spawnPrefab = new List<GameObject>();
	public List<GameObject> spawnClone = new List<GameObject>();

	void Start()
	{
		for (int i = 0; i < numberOfItemsToSpawn; i++)
		{
			if ((i < spawnPrefab.Count) && (spawnPrefab  *!= null) &&*

_ (i < spawnLocations.Count) && (spawnLocations != null))_
* {*
* // this line spawns a prefab at an objects position*
spawnClone = Instantiate (spawnPrefab , spawnLocations .transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
* }*
* }*
* }*
}

Maybe change the Start function into an IEnumerator.
Then you could create a while loop like this:

int x = 0;
while(x < numberOfItemsToSpawn)
{
     x++;
     //spawn a clone to some spawnpoint.
     
     yield return new WaitForSeconds(Random.Range(minDelay, maxDelay));
} 

Now just remember to add public float minDelay = .5f, maxDelay = 1f.

I’ve modified your code to make everything random :wink:

I hope this makes it clear how to use the Random.range method

Notes:

  • Always try to initialize your variables inside the Awake method

  • Use capital CamelCase for public variables

  • Class names with underscores are bad practice, Change it to “ShipTrafficSpawner” if possible

    public class Ship_Traffic_Spawner : MonoBehaviour
    {
    public int NumberOfItemsToSpawn = 49; // modifiable in the inspector
    public List SpawnLocations;
    public List SpawnPrefabs;
    public List SpawnClones;
    public float MinSpawnDelay;
    public float MaxSpawnDelay = 1;

     protected void Awake()
     {
         SpawnLocations = new List<Transform>();
         SpawnPrefabs = new List<GameObject>();
         SpawnClones = new List<GameObject>();
     }
     
     protected void Start()
     {
         StartCoroutine(SpawnCoroutine());
     }
    
     protected IEnumerator SpawnCoroutine()
     {
         for (int i = 0; i < NumberOfItemsToSpawn; i++)
         {
             // Get a random delay
             float delay = Random.Range(MinSpawnDelay, MaxSpawnDelay);
             // Get a random spawn location
             Transform spawnLocation = SpawnLocations[Random.Range(0, SpawnLocations.Count)];
             // Get a random prefab to spawn
             GameObject spawnPrefab = SpawnPrefabs[Random.Range(0, SpawnPrefabs.Count)];
             // Wait for the delay
             yield return new WaitForSeconds(delay);
             // Spawn the random prefab at a random location
             SpawnClones.Add((GameObject)Instantiate(spawnPrefab, spawnLocation.position, Quaternion.Euler(0, 0, 0)));
         }
     }
    

    }