enemies will not spawn for some reason

Good evening guys,

I have used a spawn script that i know works but for some reason my enemies are not spawning. I have created the empty gameobject and renamed it spawnpoint and duplicated that to create another one, i have then dragged this code on top of it:

               using UnityEngine;
              using System.Collections;


              public class spawnenemy : MonoBehaviour
               {


    
    	//array of our spawn points
    	public GameObject[] SpawnPoints;
    	//variables for spawn delay
    	private float nextSpawn = 0;
    	public float spawnRate = 2;
    	 //amount of enemies
                  public int EnemiesAmount = 20;


 


    	//Enemy prefab
    	public GameObject ZombiePrefab;


    


    


    


    	// Use this for initialization


    	void Start () {


    


    	}


    


    	// Update is called once per frame


    	void Update () {


        SpawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint");


        FirstWave();


    	}


    



    	void FirstWave(){


        for (int i=0; i<EnemiesAmount; i++)
		{


            if (Time.time > nextSpawn  EnemiesAmount > 0)
			{


            nextSpawn = Time.time + spawnRate;


            GameObject pos = SpawnPoints[Random.Range (0, SpawnPoints.Length)]; 


            Instantiate(ZombiePrefab, pos.transform.position, pos.transform.rotation);


            EnemiesAmount--;


            }


        }


   }


}

for some reason they are not generating, i have made sure i dropped the enemy prefab on it in the inspector but it does not want to move, i have added the lookat function to the enemy so when it does spawn it looks for the player, can someone suggest where i may be going wrong here. I have checked the inspector and the prefab is on there i cannot understand why they are not spawning the ememy wierd, am i missing something obvious.

kind regards

David :wink:

The tag of the spawn points needs to be “spawnpoint” not the name of the gameObject. Also, this shouldn’t work SpawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint"); Create a loop which goes through checking for all the gameObject with the tag “spawnpoint” and add it to the array. You’re almost there with what you already have.

I’d scrap this and start over. In Start(), locate and store all your spawnpoints (not in Update as that runs constantly). Then, InvokeRepeating() a function for doing the spawning (similar content to what you’re attempting with that Firstwave function).

EDIT: Minus the Time stuff - you shouldn’t need any of that stuff and it’s not going to work the way you’re implementing it at the moment.

EDIT2: Oh! And…

GameObject[] SpawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint") as GameObject[];

…is just fine.

Sorry that was very disjointed :slight_smile:

Maybe something like this?

using UnityEngine;

public class SpawnEnemy : MonoBehaviour
{
    public int enemiesMax = 20;
    public float spawnRate = 2;
    public GameObject zombiePrefab;
    private GameObject[] SpawnPoints;

    private void DoSpawnEnemy()
    {
        if (enemiesMax > 0)
        {
            GameObject pos = SpawnPoints[Random.Range(0, SpawnPoints.Length)];
            Instantiate(zombiePrefab, pos.transform.position, Quaternion.identity);
            enemiesMax--;
        }
        else
            CancelInvoke("DoSpawnEnemy");
    }

    private void Start()
    {
        SpawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint") as GameObject[];
        InvokeRepeating("DoSpawnEnemy", spawnRate, spawnRate);
    }
}

Hi Steve,

is “DoSpawnEnemy”, another gameobject or just a string, can you elaborate on this construction of this please,

kind regards

DoSpawnEnemy is a method, and its invoked using InvokeRepeating.

HI eLITEmOSSY,

I applied the code to the spawnpoint and it has been tagged but I am only getting one enemy appearing and there are no other enemies being created any ideas what may be going wrong.