Hi, i would like to stop spawning after a specific time, how should i do this? Thanks for any help,
below is my code which works:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackerSpawner : MonoBehaviour
{
[SerializeField] float minSpawnDelay = 1f;
[SerializeField] float maxSpawnDelay = 5f;
[SerializeField] Attacker[] attackerPrefabArray;
[SerializeField] float spawnerDelay = 1f;
bool spawn = true;
IEnumerator Start()
{
yield return new WaitForSeconds(spawnerDelay);
while (spawn)
{
yield return new WaitForSeconds(Random.Range(minSpawnDelay, maxSpawnDelay));
SpawnAttacker();
}
}
public void StopSpawning()
{
spawn = false;
}
private void SpawnAttacker()
{
var attackerIndex = Random.Range(0, attackerPrefabArray.Length);
Spawn(attackerPrefabArray[attackerIndex]);
}
private void Spawn(Attacker myAttacker)
{
Attacker newAttacker = Instantiate
(myAttacker, transform.position, transform.rotation)
as Attacker;
newAttacker.transform.parent = transform;
}
},Hi, I want to add in like time so that enemies stop spawning. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackerSpawner : MonoBehaviour
{
[SerializeField] float minSpawnDelay = 1f;
[SerializeField] float maxSpawnDelay = 5f;
[SerializeField] Attacker[] attackerPrefabArray;
[SerializeField] float spawnerDelay = 1f;
bool spawn = true;
IEnumerator Start()
{
yield return new WaitForSeconds(spawnerDelay);
while (spawn)
{
yield return new WaitForSeconds(Random.Range(minSpawnDelay, maxSpawnDelay));
SpawnAttacker();
}
}
public void StopSpawning()
{
spawn = false;
}
private void SpawnAttacker()
{
var attackerIndex = Random.Range(0, attackerPrefabArray.Length);
Spawn(attackerPrefabArray[attackerIndex]);
}
private void Spawn(Attacker myAttacker)
{
Attacker newAttacker = Instantiate
(myAttacker, transform.position, transform.rotation)
as Attacker;
newAttacker.transform.parent = transform;
}
}