TD spawn more than one enemy
Can anyone help me, I am trying tofigure out a way of spawning more than 1 enemy for each waves. Here is my code
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class SpawnWave : MonoBehaviour { public Wave[] waves; public SubWave[] SWave; public EnemyMovent enemy;
Wave currentWave;
SubWave SubWa;
int currentWaveNumber;
int enemiesRemainingToSpawn;
int enemiesRemainingAlive;
float nextSpawnTime;
int WaitforSpawn = 5;
void Start()
{
Invoke("NextWave", WaitforSpawn);
}
void Update()
{
Debug.Log(currentWave.enemyCount);
if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
{
enemiesRemainingToSpawn--;
nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
EnemyMovent spawnedEnemy = Instantiate(enemy);
spawnedEnemy.OnDeath += OnEnemyDeath;
}
}
void OnEnemyDeath()
{
enemiesRemainingAlive--;
if (enemiesRemainingAlive == 0)
{
Invoke("NextWave", WaitforSpawn);
}
}
void NextWave()
{
currentWaveNumber++;
print("Wave: " + currentWaveNumber);
if (currentWaveNumber - 1 < waves.Length)
{
currentWave = waves[currentWaveNumber - 1];
enemiesRemainingToSpawn = currentWave.enemyCount;
enemiesRemainingAlive = enemiesRemainingToSpawn;
}
}
[System.Serializable]
public class Wave
{
public List<SubWave> subWave = new List<SubWave>();
public int enemyCount;
public float timeBetweenSpawns;
}
[System.Serializable]
public class SubWave
{
public EnemyMovent Enemy;
public int count = 1;
}
}
Spawning 1 enemy is not a problem but I can't figure out how to do more then 1.
Answer by TBruce · Dec 26, 2016 at 09:41 PM
Here is your modified script to fix your problem (please forgive me for reformatting your code)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnWave : MonoBehaviour
{
public List<Wave> waves = new List<Wave>();
public List<SubWave> SWave = new List<SubWave>();
public EnemyMovent enemy;
Wave currentWave;
SubWave SubWa;
// [HideInInspector] // uncomment this to hide in inspector or remove the public
public int currentWaveNumber = -1;
// [HideInInspector] // uncomment this to hide in inspector or remove the public
[Range(2, 20)] // change if needeed
public int enemiesRemainingToSpawn = 5; // change this to desired value - must be greater than 0
// [HideInInspector] // uncomment this to hide in inspector or remove the public
public int enemiesRemainingAlive = 0; // needs to default to 0
// [HideInInspector] // uncomment this to hide in inspector or remove the public
public float nextSpawnTime;
// [HideInInspector] // uncomment this to hide in inspector or remove the public
[Range(0, 10)] // change if needeed
public int WaitforSpawn = 5;
void Start()
{
nextSpawnTime = Time.time;
nextSpawnTime = Time.time + 3;
Invoke("NextWave", WaitforSpawn);
}
void Update()
{
if (currentWave != null)
{
if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
{
enemiesRemainingToSpawn--;
nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
EnemyMovent spawnedEnemy = Instantiate(enemy);
enemiesRemainingAlive++;
// you may want to randomly place the spawned object somewhere on the screen like so
// Vector3 position = new Vector3(Random.Range(-9f, 9f), Random.Range(-5f, 5f), 0);
// spawnedEnemy.gameObject.transform.position = position;
spawnedEnemy.OnDeath += OnEnemyDeath;
}
}
}
void OnEnemyDeath()
{
enemiesRemainingAlive--;
enemiesRemainingToSpawn++;
if (enemiesRemainingAlive == 0)
{
Invoke("NextWave", WaitforSpawn);
}
}
void NextWave()
{
if (currentWaveNumber < waves.Count)
{
waves.Add(new Wave());
currentWaveNumber = waves.Count - 1;
currentWave = waves[currentWaveNumber];
enemiesRemainingToSpawn = currentWave.enemyCount;
}
currentWaveNumber++;
print("Wave: " + currentWaveNumber);
}
[System.Serializable]
public class Wave
{
public List<SubWave> subWave = new List<SubWave>();
public int enemyCount = 5; // you need a value here
public float timeBetweenSpawns = 3; // you need a value here
}
[System.Serializable]
public class SubWave
{
public EnemyMovent Enemy;
public int count = 1;
}
}
Since I did not have the EnemyMovent
class I have to create a dummy one with a delagate for testing purposes.
Hi, not really. In the code I can have multiple waves of enemies but I want to be able to spawn different enemies in a single wave. The code only spawns one type of enemy.
I figured out a way to do it but it is very inefficient and it might not be good for performance. I am just using it until I figure out a better way.
Sorry to hear that. I$$anonymous$$HO, I would not have gone the route that your code is written but I did not want to deviate from what you were currently using to make it incompatible with your current system nor is it evident from your original question or script (your script only has one enemy prefab) that you wanted the ability to spawn more that one type of enemies or the method (is each enemy spawned selected from a random prefab).
Also since this is only a portion of your project there is still a lot that I do not understand (e.g. the extra classes that have no real functionality, you do not have a method for destroying enemies, I do not know what your OnDeath listener looks like or how it functions - to name a few).
Happy New Year!