- Home /
Spawnerscript only spawns 5 enemy
I made a Spawn script, but it only spawns 5 enemys. Here's the script:
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies : float; // Total number of enemies to spawn.
var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
var enemiesToSpawnAtTheSameTime : int = 1;
var StartTime = 5;
var WaveWait = 5;
var WaveText : GUIText;
var WaveCount : int;
var amountEnemiesPlus = 5;
var IsSpawning : boolean;
private var wait : boolean = false;
function Start()
{
yield WaitForSeconds(StartTime);
SpawnWave();
WaweCount = 1;
amountEnemies = 15;
}
function Update()
{
if(IsSpawning == false)
{
StartWave();
}
}
function StartWave()
{
yield WaitForSeconds(WaveWait);
SpawnWave();
}
function SpawnWave()
{
if(wait) return;
if(amountEnemies > 0)
{
IsSpawning = true;
wait = true;
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++){
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
}
amountEnemies --;
wait = false;
if(amountEnemies == 0)
{
WaveCount += 1;
IsSpawning = false;
amountEnemies += amountEnemiesPlus;
}
}
}
There are no errors. Any help would be appreciated.
I will put a ---> behind the parts that i would have needed to change:
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies : float; // Total number of enemies to spawn.
var yieldTime$$anonymous$$in = 2; // $$anonymous$$inimum amount of time before spawning enemies randomly.
var yieldTime$$anonymous$$ax = 5; // Don't exceed this amount of time between spawning enemies randomly.
var enemiesToSpawnAtTheSameTime : int = 1;
var StartTime = 5;
var WaveWait = 5;
var WaveText : GUIText;
var WaveCount : int;
var amountEnemiesPlus = 5;
var IsSpawning : boolean;
/*--->*/ private var BetweenWaves : boolean;
private var wait : boolean = false;
function Start()
{
yield WaitForSeconds(StartTime);
--//SpawnWave();
| WaweCount = 1;
| amountEnemies = 7;
->SpawnWave();
}
function Update()
{
//if(IsSpawning == false)
/*--->*/if(IsSpawning == false && WaveCount > 0 && !BetweenWaves)
{
/*--->*/vBetweenWaves = true;
StartWave();
}
}
function StartWave()
{
yield WaitForSeconds(WaveWait);
/*--->*/amountEnemies += amountEnemiesPlus;
SpawnWave();
/*--->*/BetweenWaves = false;
}
function SpawnWave()
{
if(wait) return;
//if((amountEnemies > 0))
/*--->*/while(amountEnemies > 0)
{
IsSpawning = true;
wait = true;
yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax)); // How long to wait before another enemy is instantiated.
for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++){
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
}
amountEnemies --;
wait = false;
if(amountEnemies == 0)
{
WaveCount += 1;
IsSpawning = false;
//amountEnemies += amountEnemiesPlus;
}
}
}
Answer by Christian.Tucker · Feb 28, 2014 at 04:46 PM
I don't know what you mean, is it supposed to continuously spawn? If so start a CoRoutine or at the end of SpawnEnemies do something like
float waitTime = 5.0f; // 5 seconds
Invoke("SpawnWave", waitTime);
It doesn't working with Invoke. How can I do it with InvokeRepeating that if there's no more enemies in the wave it will stop for a few $$anonymous$$utes and add more enemies?
Answer by Dblfstr · Mar 03, 2014 at 01:58 PM
I looks like isSpawning will never be set to false again. In your update function, you check is isSpawning is false. And if it is false, then you run the spawn function. This will spawn your enemies. Then you immediately check if enemies==0; After your first iteration, amountEnimies should be 14 (starts at 15, then decrements by one. So amountEnimies == 0 will be false. Now the spawn function stops. However amountEnemies has to 0 before isSpawning is set bacl to false, I don't think this will ever happen. So in your update, you will only call startWave() once, isSpawning is set to true then and never reset. I think.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies : float; // Total number of enemies to spawn.
var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
var enemiesToSpawnAtTheSameTime : int = 1;
var StartTime = 5;
var WaveWait = 5;
var WaveText : GUIText;
var WaveCount : int;
var amountEnemiesPlus = 5;
var IsSpawning : boolean;
private var wait : boolean = false;
function Start()
{
yield WaitForSeconds(StartTime);
SpawnWave();
WaweCount = 1;
amountEnemies = 15;
}
function Update()
{
if(IsSpawning == false)
{
StartWave();
}
}
function StartWave()
{
yield WaitForSeconds(WaveWait);
SpawnWave();
}
function SpawnWave()
{
if(wait) return;
if(amountEnemies > 0)
{
IsSpawning = true;
wait = true;
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
for(var s : int = 0; s < enemiesToSpawnAtTheSameTime; s++)
{
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
Instantiate(obj, pos.position, pos.rotation);
}
amountEnemies --;
wait = false;
}
//Moved this part out of your: if(amountEnemies >0)
if(amountEnemies == 0)
{
WaveCount += 1;
IsSpawning = false;
amountEnemies += amountEnemiesPlus;
}
}
If I disable the is spawning after it spawned the 5 enemies it will do it again and if I do it again until the amount of enemies is 0 it's working again. Or if i set the amount of enemies to 5 by default it's working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Having camera attack upon spawning player? 1 Answer
enemy spawning when dead 1 Answer
How to import the object from server to unity 2 Answers
Need help with easy scripting 2 Answers