Enemies stop pathing once first enemy dies

I am doing the tower defense section on spawning enemies and I seem to have a problem. It looks like once the initial enemy is killed (which starts in the world under the enemies game object), new enemies stop pathing. They actually continue to spawn, but they no longer move. Also, each one that spawns takes the health of the initial prefab that was under the enemies tab. So, the last one spawns at 10 health, for example.

any hints on what might be going on here are appreciated.

Hi Jens,

Are there any error messages in your console?

yes. I get this once the gameobject with the werewolf class that starts in the world is destroyed.

I am doing something a little differently, trying to branch out a bit as I follow along, and I am dynamically finding the start point based on the spawn position. Other than that, I have been trying to get it to spawn one of three npcs, but i keep running into trouble with my knowledge there and have put a pin in it. That said, here is the script I am using for the spawner. The enemy is underneath it as you can see in this picture.

public class EnemySpawner : MonoBehaviour
{
    //[SerializeField] List<Werewolf> npcs = new List<Werewolf>();
    [Range(0.1f, 120f)]
    [SerializeField] float spawnTime = 3f;
    public Werewolf enemyPrefab;
    
    //GameObject npc;
   
    // Start is called before the first frame update
    void Start()
    {
        enemyPrefab = GetComponentInChildren<Werewolf>();
        StartCoroutine(EnemySpawnLoop());
    } 

    IEnumerator EnemySpawnLoop()
    {
        while (true)
        {
            Instantiate(enemyPrefab, transform.localPosition, Quaternion.identity);
            yield return new WaitForSeconds(spawnTime);
        }
    }

As soon as that enemy dies, that error pops up the next time an enemy is instantiated.

Did you assign a prefab from your prefab folder to the enemyPrefab field or a game object from the Hierarchy? In your screenshot, I can see that no prefab was assigned.

NullReferenceException means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty.

Currently, I am assigning it at run time via the hierarchy. I have tried so many different combinations of how I assign that field that I have lost track of how each one failed. Based on your question, I am going to try and reproduce a few of those and see if anything changes or I can at least provide more info.

Nina, setting that field directly from the prefab fixed the issue. Thank you.

I wish I could reason out how to instruct the spawner to pull from a list of enemy classes I create in the inspector, so I can have a variety of models spawning randomly, but that transition from index to instantiation is eluding me completely and has led me to where I am now. hehe

Thanks again, @Nina. Finding this has led me back to the drawing board and I switched over to an array-based approach. The result is the spawning behavior I want with zero errors.

Using an array is a good idea. Assign the different types of enemy prefabs to it in the Inspector. And if you want randomness, you could simply use Random.Range(0, yourArray.Length - 1).

[SerializeField] GameObject[] attackers;

void YourMethod() {
   int i = Random.Range(0, yourArray.Length - 1);
   Instantiate(attackers[i], ...);
}

I don’t know what you mean by list of enemy classes but as long as you can access some index, you can use Random.Range.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms