- Home /
Why won't my AI spawning script work?
It spawns 4 skeletons the first round, and everything works, but then in the second round nothing happens. What is wrong?
#pragma strict
var prefab : Transform;
var gos : GameObject[];
var dead : GameObject[];
var roundText : GUIText;
var round : int = 1;
var maxEnemies : int = 4;
var roundStart : boolean;
var count : GameObject;
var player : GameObject;
var script : boolean;
function Start ()
{
count.GetComponent(Countdown).enabled = false;
}
function Update ()
{
gos = GameObject.FindGameObjectsWithTag("Enemy");
dead = GameObject.FindGameObjectsWithTag("Dead");
player = GameObject.FindWithTag("Player");
script = player.GetComponent(PlayerHealth).isDead;
Round();
roundText.text = "Round" + round.ToString();
}
function Round()
{
if(gos.length < maxEnemies && roundStart == false && script == false)
{
roundStart = true;
for (var i : int = 0;i < 4; i++)
{
Instantiate (prefab, Vector3(i + 2 + 1048.741, 4, 1596.766), Quaternion.identity);
}
}
if (dead.length == maxEnemies)
{
round += 1;
maxEnemies += 2;
for(var k = 0 ; k < dead.length ; k ++)
{
Destroy(dead[k]);
}
count.GetComponent(Countdown).enabled = true;
roundStart = false;
}
}
Answer by KMKxJOEY1 · Mar 27, 2013 at 04:08 PM
firstly:
roundText.text = "Round" + " " + round;
lol why not just
roundText.text = "Round" + round;
also round is not a string so you must use a ToString() like this:
roundText.text = "Round" + round.ToString();
As for your problem, where are you turning roundStart
back on?
Thanks! I turn it on here:
if(gos.length < maxEnemies && roundStart == false && script == false)
{
roundStart = true;
for (var i : int = 0;i < 4; i++)
{
Instantiate (prefab, Vector3(i + 2 + 1048.741, 4, 1596.766), Quaternion.identity);
}
}
and then i make it false once they are all dead here:
if (dead.length == maxEnemies)
{
round += 1;
maxEnemies += 2;
for(var k = 0 ; k < dead.length ; k ++)
{
Destroy(dead[k]);
}
count.GetComponent(Countdown).enabled = true;
roundStart = false;
}
Answer by BlackWingsCorp · Mar 27, 2013 at 02:22 PM
Based on your code it looks like you tagged the skeletons gameobject multiple time as"Enemy" then "Dead" which is impossible because unity doesn't allow you to do that. (Unless you trick it with code) so the length of your gos array never goes under 4 because even when the skeletons are down they don't get destroyed. I assume you have an enemy health script so add something like this to it:
if(health == 0){
Destroy(this.gameObject)
}
Hope it helps.
Actually what happens is I kill each of them and then when they're all dead it deletes them. That way I can have a death animation and they stay in the ground until the round is over.