enemie spawner dont work

Tobi_e

New member
Hello, I wanted to make a spawn system, I'll add the code. I created a character and saved it as a prefab. The spawner then checks whether the enemies are still alive and should then start the next round. However, only the first round starts after I've killed the enemies, nothing happens. Could this be because the dead enemies are still lying on the ground and it doesn't restart because of that? is there a way that if i kill the enemie he does the dying animation and after 5 seconds the corpse disappears? if so, where can i write this? Thanks in advance, regards tobi

using UnityEngine;
using System.Collections;

public class wavespawner : MonoBehaviour
{

public enum SpawnState { SPAWNING, WAITING, COUNTING };

[System.Serializable]
public class Wave
{
public string name;
public Transform enemy;
public int count;
public float rate;
}

public Wave[] waves;
private int nextWave = 0;
public int NextWave
{
get { return nextWave + 1; }
}

public Transform[] spawnPoints;

public float timeBetweenWaves = 5f;
private float waveCountdown;
public float WaveCountdown
{
get { return waveCountdown; }
}

private float searchCountdown = 1f;

private SpawnState state = SpawnState.COUNTING;
public SpawnState State
{
get { return state; }
}

void Start()
{
if (spawnPoints.Length == 0)
{
Debug.LogError("No spawn points referenced.");
}

waveCountdown = timeBetweenWaves;
}

void Update()
{
if (state == SpawnState.WAITING)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}

if (waveCountdown <= 0)
{
if (state != SpawnState.SPAWNING)
{
StartCoroutine(SpawnWave(waves[nextWave]));
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}

void WaveCompleted()
{
Debug.Log("Wave Completed!");

state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;

if (nextWave + 1 > waves.Length - 1)
{
nextWave = 0;
Debug.Log("ALL WAVES COMPLETE! Looping...");
}
else
{
nextWave++;
}
}

bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy") == null)
{
return false;
}
}
return true;
}

IEnumerator SpawnWave(Wave _wave)
{
Debug.Log("Spawning Wave: " + _wave.name);
state = SpawnState.SPAWNING;

for (int i = 0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1f / _wave.rate);
}

state = SpawnState.WAITING;

yield break;
}

void SpawnEnemy(Transform _enemy)
{
Debug.Log("Spawning Enemy: " + _enemy.name);

Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(_enemy, _sp.position, _sp.rotation);
}

}
 
If the enemies are dead but still visible, this should not hinder you from spawning more enemies. You can write a script that listens on the OnDeath event, and then starts a count down. At the end you can destroy the enemy. But should also consider using the object pool for the enemies.
 
I'm sorry I'm still relatively inexperienced in programming I tried to write a script that listens to the ondeath event and then destroys the dead body but unfortunately it doesn't work can you help me to write the script? that was my first attemp

using UnityEngine;
using Opsive.Shared.Events;

public class todscript : MonoBehaviour
{
public void Awake()
{

EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);
}


private void OnDeath(Vector3 position, Vector3 force, GameObject attacker)
{
Debug.Log("The object died");
Destroy(gameObject, 1f);
}

}
 
If the enemies are dead but still visible, this should not hinder you from spawning more enemies. You can write a script that listens on the OnDeath event, and then starts a count down. At the end you can destroy the enemy. But should also consider using the object pool for the enemies.
okay the script to remove the enemie works. if he dies then he disappears afterwards but unfortunately the spawner still doesn't work i check if (GameObject.FindGameObjectWithTag("Enemy") == null) and actually if nothing is there anymore this condition is met unfortunately it still doesn't work do you maybe have an idea?
 
Top