Menu
CoddyTech

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 icon

Challenge

Easy

Send the first wave of Grunts down the path.

  1. In Wave.cs, put [System.Serializable] on Wave, with public Enemy enemy;, public int count; and public float spacing;.
  2. In WaveSpawner, add [SerializeField] private List<Wave> waves; and [SerializeField] private Transform[] waypoints;.
  3. From Start, run a coroutine that spawns waves[0].count enemies 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