Enemy AI: Infinite Spawn Point / Tower

This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here.

There is an example of an infinite enemy spawn tower which can be downloaded here.

This will set an object that will infinitely spawn objects that will chase the player. There will be a delay between each object being spawned.

Create a new prefab (it can just be a cube) to use as the spawn tower.

Add an empty gameobject named SpawnPoint, this will be the position where the spawned objects are created.

Create a script called SpawnTowerManager.

Attach this script to the SpawnTower and open it.

Add the points numbered in the code below

  1. Create a Transform variable to store the spawn point that we want the objects to be created at
  2. Create a public gameobject that will be used to store the prefab object (enemy prefab) that we want to spawn
  3. Create a Transform that will contain the position of the target that the spawned objects should move towards.
  4. This creates an IEnumerator variable which is needed so that we can set spawn delays and have multiple objects continually generated over time.
  5. This float sets the amount of time in seconds that will be between each object being created. It is set to 2 seconds by default.
  6. We create a function called SpawnEnemy. This will spawn the enemies. It takes one parameter / argument, that of the delay or wait time that should pass before spawning another object.
  7. We have an infinite loop as we want this infinite spawner to always run.
    We could replace this with a for loop if we only wanted it to run a set number of times.
  8. The yield return new WaitForSeconds() function pauses this function but allows the rest of the program to run as normal. It will wait until the time passed to WaitForSeconds has passed, in this case two seconds before continuing with the later lines of code in the SpawnEnemy function.
  9. Here we spawn the enemy object at the provided spawn point.
  10. Here we set the target of the enemy to the one stored in the Target variable created in point 3. This assumes that the object we are spawning has a script component attached to it called EnemyTargetPlayer with a public variable targetPosition.
  11. Here we create the SpawnEnemy IEnumerator with the delay.
  12. Now we start the coroutine which will run indefinitely.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnTowerManager : MonoBehaviour
    // 1. Point to spawn the enemy at from this tower
    public Transform SpawnPoint;
    // 2. Prefab of the enemy to spawn from this spawn tower
    public GameObject EnemyToSpawn;

    // 3. The object that the spawned target should aim at.
    public Transform Target;

    // 4. Ienumerator for running a coroutine to keep spawning
    private IEnumerator spawnTimer;

    // 5. The time to wait between spawning enemies from this tower.
    public float spawnDelay = 2.0f;

    // Start is called before the first frame update
    void Start()
        // 11. Run the spawner once
        spawnTimer = SpawnEnemy(spawnDelay);
        // 12. Start the coroutine to repeatedly

    // 6. Coroutine function to spawn enemies, will wait until time expires
    private IEnumerator SpawnEnemy(float waitTime)
        // 7. Always run
        while (true)
            // 8. Wait until the wait time has passed before doing anything
            yield return new WaitForSeconds(waitTime);
            // 9. Spawn / Instantiate the enemy at the location given in worldspace.
            GameObject enemyGameObject = Instantiate(EnemyToSpawn, SpawnPoint.position, Quaternion.identity);
            /* 10. 
            Set the new enemies target to the player. 
            Note that this assumes there is a script on the enemy gameobject called
            EnemyTargetPlayer with a public variable targetPosition
            enemyGameObject.GetComponent<EnemyTargetPlayer>().targetPosition = Target;

Save the script and place the spawn tower prefab on the scene.

Now we need to drag the enemy prefab onto the Enemy To Spawn box and the player target to the target box.

Test the scene.

Try adding multiple spawn towers.

You have now created an infinite spawn tower.