This repository has been archived on 2026-04-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2019-03-08 09:54:14 +11:00

28 lines
802 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimedSpawner : MonoBehaviour {
//inspector access elements
public GameObject prefab;
public float spawnDelay = 10f;
public int maxSpawns = 3;
//private members
float lastSpawnTime = float.NegativeInfinity;
List<GameObject> spawnList = new List<GameObject>();
//TODO: maybe all spawners should shut off (and monsters become inactive) when the player is too far away.
void Update() {
//prune the list
spawnList.RemoveAll(spawn => spawn == null);
//spawn if enough time has passed
if (Time.time - lastSpawnTime > spawnDelay && spawnList.Count < maxSpawns) {
lastSpawnTime = Time.time;
spawnList.Add(Instantiate(prefab, transform.position, Quaternion.identity, transform));
}
}
}