- Home /
Fixedish
New spawn help
I have system for the way the enemies spawn, but it doesn't spawn correctly, here is the code
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public static int EnemiesAlive;
public Wave[] waves;
public Transform SpawnPoint;
public float TimeBetweenWaves = 7f;
private float countdown = 3f;
public Text waveCountdownText;
public GameManager gameMaster;
private int WaveIndex = 0;
void Start ()
{
EnemiesAlive = 0;
}
void Update()
{
if (EnemiesAlive > 0)
{
return;
}
if (WaveIndex == waves.Length)
{
gameMaster.WinLevel();
this.enabled = false;
}
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = TimeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountdownText.text = string.Format("{0:00.00}", countdown);
}
IEnumerator SpawnWave()
{
PlayerStats.Rounds++;
Wave wave = waves[WaveIndex];
EnemiesAlive = wave.count;
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
WaveIndex++;
}
void SpawnEnemy(GameObject enemy)
{
Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation);
}
}
I want them to spawn when the previous wave dies
they all spawn like this yellow, 2 blue 1 green 3 blue together in one wave, instead it should be one blue until it dies, 2 blue until that wave dies, 3 blue until that wave dies, 3 green until that have dies and after 10 yellow are spawned and killed the level should end
I can't read that can you post your code properly formatted?
I would have a variable waveSize or numberOfEnenies and then every time you kill an enemy the value decreases then when it is 0 you start the next wave.
I have a way to do that for the enemies alive to go down
But what happens? Does it not spawn in or does it spawn in too quickly or something else?
they all spawn like this yellow, 2 blue 1 green 3 blue together in one wave, instead it should be one blue until it dies, 2 blue until that wave dies, 3 blue until that wave dies, 3 green until that have dies and after 10 yellow are spawned and killed the level should end
One last question, is there any reason why wave 0, 1, and 2 have different prefabs?
@gawynross where should I put that? also the EnemiesAlive = WaveIndex doesn't work
You would do EnemiesAlive = waves[WaveIndex].enemyCount (enemyCount or something). But, you closed the question so I guess you fixed it. Although, I am curious so…, what was the solution?
I added a ton of waves and since they all spawned at once it made it playable, I would have liked it to work the normal way but I did what I had to
Answer by gawynross · May 22, 2021 at 05:19 PM
The only thing I can think of is that your EnemiesAlive count is going up as you spawn in the enemies. It should be set as the total amount of enemies right away. I’ll keep looking for other solutions if this doesn’t work.
@gawynross I tried adding EnemiesAlive = waveIndex and that didn't work, people on replit said it was because the EnemiesAlive was static
Ah, so did that solution work?
If it didn’t I think you would want to do EnemiesAlive = waves[WaveIndex].Count; I don’t know your variable name for the total enemies in a wave, so I will just assume it is Count.
I could also bump of some setting like crazy and make it so if enemies alive = 0 then you win level, lives would go down to 5