- Home /
Spawn waves & wait till it's killed, spawn again.
Hey, I want to make wave spawning script. I don't want to just spawn waves in the loop because after some time there will be 9999 enemies. I want to spawn a wave of monsters and wait till player kills them then spawn next wave. I was thinking about making a list of enemies, add enemies to it and when it's empty then spawn another wave. The problem is I have 3 enemies and player would have to kill all three of them at once to spawn another wave? I wanted to start the coroutine in an update function. Also I wanted to ask if I can add GAMEOBJECTS to the list if they ARE NOT on the scene? The code would look like this:
public GameObject Enemy1;
public GameObject Enemy2;
public GameObject Enemy3;
public Vector3 spawner1Pos = new Vector3(-36.5f, 3.67f, 88.2f);
public List<GameObject> enemies = new List<GameObject>();
private void Start()
{
enemies.Add(Enemy1);
enemies.Add(Enemy2);
enemies.Add(Enemy3);
}
private void Update()
{
if(enemies.Count == 0)
{
for(int i = 0; i < 3; i++)
{
Instantiate(Enemy1, spawner1Pos, Quaternion.identity);
}
}
}
It's just an example code but it would look like this.
Answer by Vega4Life · Dec 15, 2018 at 07:08 PM
Here is an idea with a singleton spawn manager. Just use it for some reference, but basically, when an enemy dies, you send a notification into the manager. The manager will determine when to spawn more. It also spawns the first wave of enemies too. Should give you some ideas.
// Place in your scene somewhere
public class SpawnManager : MonoBehaviour
{
// Singleton
static SpawnManager instance;
public SpawnManager Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<SpawnManager>();
}
return instance;
}
}
[SerializeField] GameObject enemyPrefab; // Link the enemy prefab to make clones of -- this could be a list of prefabs that you randomly choose from
[SerializeField] int spawnCount = 5; // as many as you want
int currentEnemyCount;
// Our property will take care of spawning as it decrements
public int CurrentEnemyCount
{
get { return currentEnemyCount; }
set
{
currentEnemyCount = value;
if (currentEnemyCount <= 0)
{
CreateEnemies();
}
}
}
void Awake()
{
CreateEnemies();
}
void CreateEnemies()
{
for (int i = 0; i < spawnCount; i++)
{
Instantiate(enemyPrefab); // Add in spawn locations - whatever you want
currentEnemyCount++;
}
}
/// <summary>
/// CALL THIS FROM ENEMY SCRIPT WHEN IT DIES
/// The call would look like SpawnManager.Instance.OnEnemyDeath();
/// </summary>
public void OnEnemyDeath()
{
// As this gets to zero, it will spawn more through the property
CurrentEnemyCount--;
}
Hey Thanks for your answer but I made some code myself also before your answer and I want you to take a look, if you can of course, and see if you can help me with the problem I have with this code... I have to add, I wanted to make more "advanced" wave system with enemies increasing each wave etc. I exceeded 3000 characters here so I can't add the answer so I will just link the screenshots, they're from imgbb. There are 10 parts of them :/