- Home /
I need my object to spawn after it dies
I have to keep spawning enemies after the main enemy dies, here is the code using System.Collections; using UnityEngine; using UnityEngine.UI; public class WaveSpawner : MonoBehaviour { public Transform enemyPrefab; public Transform SpawnPoint; public float TimeBetweenWaves = 6.5f; private float countdown = 3f; public Text waveCountdownText; private int WaveIndex = 0; void Update() { if (countdown <= 0f) { StartCoroutine(SpawnWave()); countdown = TimeBetweenWaves; } countdown -= Time.deltaTime; waveCountdownText.text = Mathf.Round(countdown).ToString(); } IEnumerator SpawnWave() { WaveIndex++; for (int i = 0; i < WaveIndex; i++) { SpawnEnemy(); yield return new WaitForSeconds(0.7f); } } void SpawnEnemy() { Instantiate(enemyPrefab, SpawnPoint.position, SpawnPoint.rotation); } }
Answer by Ytsorf · May 13, 2021 at 07:27 PM
Hi there try to use this:
`
void SpawnEnemy() {
GameObject enemy = Instantiate(enemyPrefab, SpawnPoint.position, SpawnPoint.rotation);
}
`
in this way you can destroy and spawn any enemies you want, hope it help <3
Fyi, if you are going to spawn lots of enemies it's probably better to create a pool of them and then disable the game object instead of deleting it.
I need them to spawn in waves etc 1 enemy 1 enemy 2 enemy 3 enemy etc
hey @Ytsorf where should I add this, also can you edit and leave the full code down below, thanks. the code doesn't work
well, if you want to create a system of Waves and enemies thats gonna be a lil tricky thing to do but its simple, you make a List where you gonna store the prefabs, a bool that will check if all enemies are kill/destroy,an int that will store the value of the wave(UI), a void to spawn enemies and a IEnumerator that will use all that to check if the enemies are kill and spawn the next wave with foreach.
thanks @Ytsorf, please leave the done down below that would help me so much, thanks -Bunnytoes