How do I spawn a List of Objects, so that there is a delay after each object that spawned?
Hi, I've been trying to make a script which spawns Objects from a List randomly in a 10*10 field. This part works and now I try to add a delay of 0.25 seconds after each object that spawned.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] MyObjects;
float spawnCD = 0.25f;
float spawnCDremaining = 0f;
// Use this for initialization
void Start()
{
spawnCDremaining -= Time.deltaTime;
if (spawnCDremaining < 0)
{
spawnCDremaining = spawnCD;
SpawnList();
}
}
public void SpawnList()
{
foreach (GameObject spawnObject in MyObjects)
{
Spawn(spawnObject);
}
}
public void Spawn(GameObject i)
{
float Min = -5f, Max = 5f;
Vector3 newPos = new Vector3(Random.Range(Min, Max), 10, Random.Range(Min, Max));
Instantiate(i, transform.parent.localPosition + newPos, Quaternion.identity);
}
}
When I put the cooldown part in an update function instead of the start function, it spawns ALL objects every 0.25 seconds, but thats not what I want.
Comment