A Wave
Lesson 5 of 25 in Coddy's Unity Game: Tower Defense course.
The Grunt and the Runner are prefabs now, with a Tank and a Boss, and a Spawner stands on WP0. A wave is a few numbers: which enemy, how many, how far apart. So Wave is a [System.Serializable] class, and the spawner keeps a List<Wave> that the Inspector fills with the game's eight waves. Without [System.Serializable] Unity cannot fill the list, and it stays empty.
Spawning with gaps is a coroutine: yield return new WaitForSeconds(...) waits without stopping the game. A prefab cannot point at the waypoints in the scene, so the spawner hands each new enemy its own right after Instantiate:
Enemy enemy = Instantiate(wave.enemy, waypoints[0].position, Quaternion.identity);
enemy.waypoints = waypoints;
yield return new WaitForSeconds(wave.spacing);Challenge
EasySend the first wave of Grunts down the path.
- In
Wave.cs, put[System.Serializable]onWave, withpublic Enemy enemy;,public int count;andpublic float spacing;. - In
WaveSpawner, add[SerializeField] private List<Wave> waves;and[SerializeField] private Transform[] waypoints;. - From
Start, run a coroutine that spawnswaves[0].countenemies as above.
Press Play: six Grunts march out, 1.2 seconds apart. Then Check.
Try it yourself
using UnityEngine;
// Comes later: sends the enemies down the path in waves.
public class WaveSpawner : MonoBehaviour
{
}
All lessons in Unity Game: Tower Defense
Practice on your own: Unity playground