- Home /
Spawn multiple enemies from spawn point
Hello, I have a simple spawn script but when I added the var enemy2 : Game Object, it does not want to spawn enemy 2. It will only spawn enemy 1.
var Enemy : GameObject;
var Enemy2 : GameObject;
var Enemy3 : GameObject;
var Enemy4 : GameObject;
var SpawnPoint : GameObject;
//spawn enemy starting in 1 second every 5 seconds
InvokeRepeating("SpawnEnemy", 1, 5);
function SpawnEnemy()
{
Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity);
}
As you can see I have 4 different enemies to spawn but it will only spawn enemy 1. Sorry I am new to this and learning. Thank you.
Answer by cdrandin · Aug 10, 2013 at 06:03 PM
You could try var Enemies : GameObjects[]
not sure of array instantiation for JS. Then just set your indexes of which you want to spawn.
You will need an update function so you know what enemy you would like to spawn, whether it be random or not.
Currently it does spawn fine, but how can I get the others to spawn at the same time? So it would be 4 enemies spawning every 5 seconds.
Answer by Aevek · Aug 10, 2013 at 09:09 PM
I don't really work in javascript, but I think that for what you're asking for, something like this should work. It's not the tidiest, but it should serve to spawn 4 enemies every 5 seconds. Probably.
function Update()
{
while(true)
{
yield WaitForSeconds(5);
SpawnEnemies();
}
}
function SpawnEnemies(){
Instantiate (Enemy, SpawnPoint.transform.position + transform.forward, Quaternion.identity);
Instantiate (Enemy, SpawnPoint.transform.position - transform.forward, Quaternion.identity);
Instantiate (Enemy, SpawnPoint.transform.position + transform.right, Quaternion.identity);
Instantiate (Enemy, SpawnPoint.transform.position - transform.right, Quaternion.identity);
}
Answer by mjr711 · Aug 11, 2013 at 06:06 AM
Thanks Aevek, that code was able to spawn 2 enemies but I am still unable to spawn 4.
Here is how the code is now.
var Enemy : GameObject; var Enemy2 : GameObject; var Enemy3 : GameObject; var Enemy4 : GameObject; var SpawnPoint : GameObject;
//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy", 1, 5);
function SpawnEnemy() { Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity); }
function Update() { while(true) { yield WaitForSeconds(5); SpawnEnemies(); } }
function SpawnEnemies(){ Instantiate (Enemy, SpawnPoint.transform.position + transform.forward, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position - transform.forward, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position + transform.right, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position - transform.right, Quaternion.identity); }
Any ideas on what I am doing wrong? Thank you.
Answer by InfernoZYB · Sep 28, 2013 at 03:58 PM
Havent tested this but i think this sould work. I think the problem is you have it spawn Enemy not like enemy2 and ect here is the code:
var Enemy : GameObject;
var Enemy2 : GameObject;
var Enemy3 : GameObject;
var Enemy4 : GameObject;
var SpawnPoint : GameObject;
//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy", 1, 5);
function SpawnEnemy() { Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity); }
//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy2", 1, 5);
function SpawnEnemy2() { Instantiate (Enemy2, SpawnPoint.transform.position, Quaternion.identity); }
//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy3", 1, 5);
function SpawnEnemy3() { Instantiate (Enemy3, SpawnPoint.transform.position, Quaternion.identity); }
//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy4", 1, 5);
function SpawnEnemy4() { Instantiate (Enemy4, SpawnPoint.transform.position, Quaternion.identity); }