Hi guys,
For the last couple of months I have been going through the forums on here how to generate a spawn of enemy a certain amount of time from different locations even reading some of these replies its not making sense. I have an enemy that comes towards me and shoots at me which is fine, when my player moves towards him it explodes and is removed. What I need to know or a short script is
a) how to create a spawn point
b) how do I get my enemy to keep coming out of these random spawn points say every 2 seconds
c) once I have killed them I will load to another scene
I am not asking for it to be written word by word but I am just not understanding it maybe i just cannot get this i do not know, any help would be appreciated, what goes where;-)
a) I like to create an empty gameobject and attach script to that. Then use the position of the gameobject as the spawn point. That makes it easy to move and you can just attach to new game objects to create multiple spawn points.
b) Use a timer which subtracts time.deltaTime each update and once it get > 2seconds spawn and reset. Then use instantiate to create a new prefab which will be your bad guy with AI scripts etc attached.
c) Keep global counter and when the counter > target kills, load the new scene.
Ok, here we go!
C# version
using UnityEngine;
using System.Collections;
public class SpawnController : 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 EnemyPrefab;
// 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(EnemyPrefab, pos.transform.position, pos.transform.rotation);
EnemiesAmount--;
}
}
}
}
JS version
#pragma strict
//array of our spawn points
var SpawnPoints : GameObject[];
//variables for spawn delay
var nextSpawn = 0;
var spawnRate = 2;
//amount of enemies
var EnemiesAmount = 20;
//Enemy prefab
var EnemyPrefab : GameObject;
// Update is called once per frame
function Update () {
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
FirstWave();
}
function FirstWave(){
for (var i=0; i<EnemiesAmount; i++)
{
if (Time.time > nextSpawn EnemiesAmount > 0)
{
nextSpawn = Time.time + spawnRate;
var pos = SpawnPoints[Random.Range (0, SpawnPoints.Length)];
Instantiate(EnemyPrefab, pos.transform.position, pos.transform.rotation);
EnemiesAmount--;
}
}
}
Attach this script to any GO in your scene and assign the variables.
Also if you want to load next level after all enemies are dead, you should make another int variable aquals to the WaveAmount, and decrease it after each enemy dead.
hi guys thnks for the response could you put this in Javascript by anychance, does this script get attached to the enemy or the empty spawn game objects i create for the spawnpoints that is what is confusing me as well, thanks for the reply sorry if i sound thick guys, also GameObject[ ] SpawnPoints; do for instance spawn1, spawn2 do i have to put them in the array box i take it [ ]
Script edited. Added js version.
- Add it to the empty GameObject.
- Create and assign ‘SpawnPoint’ tag to all your spawnpoints.
- Assign enemy prefab.
Thanks nbg_yalta i really appreciate you and everyones help on here 
I have applied this script but the enemy is not moving it is just static where am i going wrong been trying to work ths out do i have to apply some sort of lookat function to the code, any help from somebody would be great, cheers people
We can not see how you attack/chase the player. So we can’t help without any idea on whats going on. Please post as much information as possible to get the best answer.
Hi Elite and other people i have added the below code to a newly created empty gameobject which is called SpawnPoint and it has been tagged in the inspector i have saved it and ther are no errors. I have created a enemyspawn.cs file and as per the code below and saved it and no errors, i have then dragged the file on to the SpawnPoint and to the right enemy prefab is empty, so i have attached my enemy in the slot. When i run my game there is no enemy and there is no spawning of enemy. Can anyone tell me whey this is not spawning as when i used the one here in Javascript it wored fine with out any issues. Also do i need to provide a lookat target to the enemy as well so when they spawn they do not bunch up as such 
using UnityEngine;
using System.Collections;
public class enemyspawn : 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 EnemyPrefab;
// 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(EnemyPrefab, pos.transform.position, pos.transform.rotation);
EnemiesAmount--;
}
}
}
}
I have managed to get the enemy on the screen but now it it will not spawn i need some advice on this is there any reason why they just will not generate enemies, please help guys 