- Home /
Having trouble with an enemy spawn script
I'm trying to create an enemy spawn script that starts off slowly spawning enemies, and then slowly ramps up after each minute...as well as has a "large wave" of enemies at a certain time.
Everything runs smoothly for the first minute, and it feels like it transitions nicely into the second minute where the level increases, but for some reason there seems to be two seperate timers for the "SecondlevelSpawn". And it will then just start spitting out 1 extra wave of Second level spawns.
I've been at this for about two days now, and I can't find out why this is happening. Hopefully another set of eyes on this will help!
#pragma strict
static var playerDamage: float = 0; //records the damage the player has taken
var waveActive: boolean = true; //toggles for controlling the wave
var spawnEnemies: boolean = true; //
var bigSpawn: boolean = false;
var firstWaveActive: boolean = true;
var secondWaveActive: boolean = false;
var healthCount: int = 10; //players health count
var waveText: UILabel; //ui label
var healthText: UILabel;
var waveLevel: int = 0; //current wave we are on
var enemyPrefabs: GameObject[]; //array that contains all the enemy types
var enemyPrefabs02: GameObject[];
var alienSpawnPoints: Transform[]; //contains the spawn points
var respawnMinBase: float = 3.0; //used to ramp up the amount of spawns
var respawnMaxBase: float = 10.0; //
private var respawnMin: float = 3.0; //
private var respawnMax: float = 10.0; //
var difficulty: int = 0;
var seconds: int = 0;
var minutes: int = 0;
function Start ()
{
}
function Update ()
{
Clock ();
if (waveActive)
{
spawnEnemies = false;
FinishWave ();
}
if ( minutes >= 1 && firstWaveActive )
{
firstWaveActive = false;
secondWaveActive = true;
print ( "swapping over to second wave level");
waveActive = true;
}
if ( waveLevel == 09 && secondWaveActive )
{
secondWaveActive = false;
bigSpawn = true;
waveActive = true;
}
}
function SetNextWave () //prepares the values for the next wave (how many enemies will be spawn)
{
waveLevel ++; //ups the wave level
}
function StartNewWave ()
{
UpdateHUD (); //updates the gui
NewSpawnEnemy (); //spawns new enemy
waveActive = true; //sets the toggle to spawn enemies
spawnEnemies = true;
}
function UpdateHUD ()
{
waveText.text = "Wave: " +waveLevel; //updates the current waveLevel to the wave GUI
healthText.text = "Health " +healthCount; //updates the current healthLevel to the health GUI
}
function FinishWave ()
{
waveActive = false;
var i: int;
if ( minutes <= 1 && firstWaveActive && !secondWaveActive )
{
spawnEnemies = false;
i = Random.Range (20, 30);
print ("under 1 minute, waiting for "+i);
yield WaitForSeconds (i); //waits for the the intermission time to pass
SetNextWave (); //sets the next wave command
StartNewWave ();
}
if ( minutes >= 1 && !firstWaveActive && secondWaveActive)
{
spawnEnemies = false;
i = Random.Range (12, 18);
print ("over 1 minute, waiting for "+i);
yield WaitForSeconds (i); //waits for the the intermission time to pass
SetNextWave (); //sets the next wave command
StartNewWave ();
}
}
function NewSpawnEnemy ()
{
if ( minutes <= 1 && firstWaveActive && !bigSpawn )
{
FirstLevelSpawn ();
print ("first level spawn happening");
}
if ( minutes >= 1 && !bigSpawn && secondWaveActive && !firstWaveActive )
{
SecondLevelSpawn ();
}
if ( bigSpawn && !secondWaveActive )
{
BigSpawn ();
print ("big spawn happening");
}
}
function FirstLevelSpawn ()
{
var i = difficulty;
for (; i >= 0; i--)
{
var r = Random.Range (1, 2);
var enemyChoice = Random.Range (0, enemyPrefabs.Length);
var spawnChoice : int;
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
yield WaitForSeconds (r);
}
print ( "first level spawn finished");
}
function SecondLevelSpawn ()
{
var i = difficulty;
for (; i >= 0; i--)
{
var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
var spawnChoice : int;
if ( enemyChoice == 1 )
{
var a = 1;
for (; a >= 0; a--)
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
if ( enemyChoice == 0 )
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
print ( "second level spawn finished" );
}
function BigSpawn ()
{
print ("Big Spawn!");
var i = 3;
for (; i >= 0; i--)
{
var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
var spawnChoice : int;
if ( enemyChoice == 1 )
{
var a = 1;
for (; a >= 0; a--)
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
if ( enemyChoice == 0 )
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
print ( "big spawn loop "+i);
}
bigSpawn = false;
secondWaveActive = true;
print ("big spawn finished");
}
function Clock ()
{
seconds = Time.time % 60;
minutes = Time.time / 60;
}
It's kinda bouncing around and hard for me to follow but I'm guessing one issue is that you've declared seconds and $$anonymous$$utes as Int but are dividing by a float (time.time), don't know how UnityScript handles that. In one of all those tests you have for $$anonymous$$ute>=1 or $$anonymous$$ute
var r = Random.Range (1, 2);
will always return 1 since a Random.Range on Integers excludes the max, though I see nowhere that matters in your code per se, just pointing it out.
Wel, when I test it, I put a ton of print statements in it, to see what's happening when. It seems to double up on the second wave. Other than that, it seems to be working ok. Thanks for the tip on Random.Range. So you can't see anything in there that would make it play out
Answer by Rick74 · Aug 27, 2013 at 06:16 PM
if anyone is interested, I've solved this (at least for now) by adding in a empty float private var waveEndTime.
waveEndTime = Time.time + waveLength;
with this, I've added it to my update so now nothing will happen unless Time.time >= waveEndTime.
so my update now looks like this;
function Update ()
{
Clock ();
if (waveActive)
{
if ( Time.time >= waveEndTime )
{
spawnEnemies = false;
FinishWave ();
print ( "shutting off wave");
}
}
if ( minutes >= 1 && firstWaveActive )
{
firstWaveActive = false;
secondWaveActive = true;
print ( "swapping over to second wave level");
waveLength = 5;
}
if ( waveLevel == 09 && secondWaveActive )
{
secondWaveActive = false;
bigSpawn = true;
print ( "swapping over to big spawn");
waveLength = 40;
FinishWave ();
}
if ( minutes >= 4 )
{
difficulty = minutes - 2;
}
}
I feel this has slowed things down to a reasonable pace that I can somewhat control now. However, I do feel like this whole spawner could have been made much more efficiently, and less complicated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113036im_/https://answers.unity.com/themes/thub/images/avi.jpg)