---
title: "Lesson 4.4 - For-Loops For Waves"
content_type: tutorial
url: "https://learn.unity.com/tutorial/lesson-4-4-for-loops-for-waves?language=en"
language: en
difficulty: beginner
duration_minutes: 50
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/tutorial/lesson-4-4-for-loops-for-waves.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/lesson-4-4-for-loops-for-waves.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/tutorial/lesson-4-4-for-loops-for-waves.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/tutorial/lesson-4-4-for-loops-for-waves.md?language=en&version=2021.3"
last_updated: "2026-08-04T14:21:26.000Z"
---

# Lesson 4.4 - For-Loops For Waves

Configure a wave-based Spawn Manager that increases enemy counts and spawns powerups each round.

**Overview:**

We have all the makings of a great game; A player that rolls around and rotates the camera, a powerup that grants super strength, and an enemy that chases the player until the bitter end. In this lesson we will wrap things up by putting these pieces together!

First we will enhance the enemy spawn manager, allowing it to spawn multiple enemies and increase their number every time a wave is defeated. Lastly we will spawn the powerup with every wave, giving the player a chance to fight back against the ever-increasing horde of enemies.

**Project Outcome:**

The Spawn Manager will operate in waves, spawning multiple enemies and a new powerup with each iteration. Every time the enemies drop to zero, a new wave is spawned and the enemy count increases.

## Write a for-loop to spawn 3 enemies

We should challenge the player by spawning more than one enemy. In order to do so, we will repeat enemy instantiation with a loop.

[Video](https://connect-mediagw.unity.com/h1/20241209/videos/fd8816dc-15b8-4d63-912b-a7fd8cb8d946_CWC_U4.L4.S1_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/07c3c6fa-d0e5-443c-bf8c-09bc43c4ca19/versions%5B_key==%22hZGUzJc5TfSRjIpWBB4AQ5%22%5D.sections%5B_key==%224efpH3AVCNp2JmxqIPU2FM%22%5D.body%5B_key==%224efpH3AVCNp2JmxqIPU2GU%22%5D.subtitles/CWC_U4.L4.S1_U6.mp4_20260505_045753.srt))

1. In SpawnManager.cs, in **_Start()_**, replace single **Instantiation** with a **for-loop** that spawns 3 enemies
2. Move the for-loop to a new **_void SpawnEnemyWave()_** function, then call that function from **_Start()_**

![](https://connect-mediagw.unity.com/h1/20190523/learn/images/571b4746-bcc1-4cd1-a263-653918f51c50_4_4_1.png)

## Give the for-loop a parameter

Right now, SpawnEnemyWave spawns exactly 3 enemies, but if we’re going to dynamically increase the number of enemies that spawn during gameplay, we need to be able to pass information to that method.

[Video](https://connect-mediagw.unity.com/h1/20241209/videos/956153fe-9da1-4890-8b85-af6174c23a8a_CWC_U4.L4.S2_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/07c3c6fa-d0e5-443c-bf8c-09bc43c4ca19/versions%5B_key==%22hZGUzJc5TfSRjIpWBB4AQ5%22%5D.sections%5B_key==%224efpH3AVCNp2JmxqIPU2OO%22%5D.body%5B_key==%224efpH3AVCNp2JmxqIPU2PW%22%5D.subtitles/CWC_U4.L4.S2_U6.mp4_20260505_045815.srt))

1. Add a parameter **_int enemiesToSpawn_** to the **_SpawnEnemyWave_** function
2. Replace **_i \< \_\__** with **_i \< enemiesToSpawn_**
3. Add this new variable to the function call in **_Start()_**: **_SpawnEnemyWave(\__\_);_**

![](https://connect-mediagw.unity.com/h1/20190523/learn/images/29e8026a-7ea3-4df6-bd8c-a46d834f9efa_4_4_2.png)

## Destroy enemies if they fall off

Once the player gets rid of all the enemies, they’re left feeling a bit lonely. We need to destroy enemies that fall, and spawn a new enemy wave once the last one is vanquished!

[Video](https://connect-mediagw.unity.com/h1/20241209/videos/c47e9d9d-9472-488a-823e-38e0d2b6809a_CWC_U4.L4.S3_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/07c3c6fa-d0e5-443c-bf8c-09bc43c4ca19/versions%5B_key==%22hZGUzJc5TfSRjIpWBB4AQ5%22%5D.sections%5B_key==%224efpH3AVCNp2JmxqIPU2Z7%22%5D.body%5B_key==%224efpH3AVCNp2JmxqIPU2aF%22%5D.subtitles/CWC_U4.L4.S3_U6.mp4_20260505_045827.srt))

1. In Enemy.cs, **destroy** the enemies if their position is less than a **-Y value**
2. In SpawnManager.cs, declare a new **_public int enemyCount_** variable
3. In **_Update()_**, set **_enemyCount = FindObjectsByType\<Enemy>(FindObjectsSortMode.None).Length;_**
4. Write the **if-statement** that if **_enemyCount == 0_** then **_SpawnEnemyWave_**, then delete it from **_Start()_**

![](https://connect-mediagw.unity.com/h1/20241210/learn/images/9d0ce31f-5136-4ad0-8518-521a98793769_Screenshot_2024-12-09_at_11.39.35_PM.png)

## Increase enemyCount with waves

Now that we control the amount of enemies that spawn, we should increase their number in waves. Every time the player defeats a wave of enemies, more should rise to take their place.

[Video](https://connect-mediagw.unity.com/h1/20241209/videos/9b85db0a-e2bf-4a93-9e1b-11555cf84fc1_CWC_U4.L4.S4_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/07c3c6fa-d0e5-443c-bf8c-09bc43c4ca19/versions%5B_key==%22hZGUzJc5TfSRjIpWBB4AQ5%22%5D.sections%5B_key==%224efpH3AVCNp2JmxqIPU2mf%22%5D.body%5B_key==%224efpH3AVCNp2JmxqIPU2nn%22%5D.subtitles/CWC_U4.L4.S4_U6.mp4_20260505_045848.srt))

1. Declare a new **_public int waveNumber = 1;_**, then implement it in **_SpawnEnemyWave(waveNumber);_**
2. In the if-statement that tests if there are 0 enemies left, **increment waveCount** by 1

![](https://connect-mediagw.unity.com/h1/20241210/learn/images/55b5c8ef-b6f1-4d6d-be04-d5d3fc3dfb4b_Screenshot_2024-12-09_at_11.41.15_PM.png)

## Spawn Powerups with new waves

Our game is almost complete, but we’re missing something. Enemies continue to spawn with every wave, but the powerup gets used once and disappears forever, leaving the player vulnerable. We need to spawn the powerup in a random position with every wave, so the player has a chance to fight back.

[Video](https://connect-mediagw.unity.com/h1/20241218/videos/fcfc398e-3b31-4a63-a421-8fc0a59170e6_CWC_U4.L4.S5_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/07c3c6fa-d0e5-443c-bf8c-09bc43c4ca19/versions%5B_key==%22hZGUzJc5TfSRjIpWBB4AQ5%22%5D.sections%5B_key==%224efpH3AVCNp2JmxqIPU2u0%22%5D.body%5B_key==%224efpH3AVCNp2JmxqIPU2v8%22%5D.subtitles/CWC_U4.L4.S5_U6.mp4_20260505_045902.srt))

1. In SpawnManager.cs, declare a new **_public GameObject powerupPrefab_** variable, assign the **prefab** in the inspector and **delete** it from the scene
2. In **Start()**, **Instantiate** a new Powerup
3. Before the **_SpawnEnemyWave()_** call, **Instantiate** a new Powerup

![](https://connect-mediagw.unity.com/h1/20190523/learn/images/203ca635-b3e2-45a2-bce8-f72738e0fac3_4_4_5.png)

## Lesson Recap

## New Functionality:

- Enemies spawn in waves
- The number of enemies spawned increases after every wave is defeated
- A new power up spawns with every wave

## New Concepts and Skills**:**

- For-loops
- Increment (++) operator
- Custom methods with parameters
- FindObjectsByfType

## Check your scripts:

SpawnManager.cs

```csharp
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public GameObject enemyPrefab;
    public int enemyCount;
    public int waveNumber = 1;
    public GameObject powerupPrefab;
    private float spawnRange = 9;

    void Start()
    {
        SpawnEnemyWave(waveNumber);
        Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
    }

    void Update()
    {
        enemyCount = FindObjectsByType<Enemy>(FindObjectsSortMode.None).Length;
        if (enemyCount == 0) 
        {
            waveNumber++;
            SpawnEnemyWave(waveNumber);
            Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
        }
    }

    private Vector3 GenerateSpawnPosition()
    {
        float spawnPosX = Random.Range(-spawnRange, spawnRange);
        float spawnPosZ = Random.Range(-spawnRange, spawnRange);
        Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
        return randomPos;
    }

    void SpawnEnemyWave(int enemiesToSpawn)
    {
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(enemyPrefab, GenerateSpawnPosition(),
            enemyPrefab.transform.rotation);
        }
    }
}

Enemy.cs
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 3.0f;
    private Rigidbody enemyRb;
    private GameObject player;

    void Start()
    {
        enemyRb = GetComponent<Rigidbody>(); 
        player = GameObject.Find("Player");
    }

    void Update()
    {
        Vector3 lookDirection = (player.transform.position - transform.position).normalized;
        enemyRb.AddForce(lookDirection * speed);
        if (transform.position.y < -10) 
        { 
            Destroy(gameObject); 
        }
    }
}
```
