- Home /
Help have script know how many enemies are in the world
Hello, I am making this game where once a wave of enemies dies a new wave appears. How do I code it so that my script on my spawner knows how many enemies are in the world and once they die, spawns new enemies.
Here is my spawn code
function SpawnEnemy()
{
//Instantiate the enemy prefab
if (Spawn == true){
var enemyClone : GameObject = Instantiate(enemy, transform.position, Quaternion.identity);
}
}
You could use an array of data containing spawned enemies, then when an enemy dies it is "popped" out of the array, you can then repopulate it with enemies when it's empty.
that's easy give enemy class a Static integer
and when enemy is instantiated give that integer ++
and when you want to know how many there are just ask any enemy class
Answer by Anxo · Jan 27, 2013 at 09:37 PM
create a list, every time you spawn an enemy, add them to the list, every time you kill one, remove them from the list.
That is if you need to know it live.. if you just need to know it when you hit a button, just create an array with all the enemys in the sceen by declaring an array based on a tag and then count the array.
private List<GameObject> enemies;
enemies.Add(enemyClone);
Debug.Log(enemies.size);
Or if you tag your enemy game object with the tag "enemy" you use this. But do not use this on every frame b/c it is expansive to use the Find command.
void FindEnemies(){
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
Debug.Log(enemies.length);
}
I am getting alot of errors from this, is it in Javascript? Do I put it in its own code?
I was just trying to give you the idea, I did not test the code so it might not even work in C# The Javascript version of this would be.
function FindEnemies(){
var enemies : GameObject[] =
GameObject.FindGameObjectsWithTag("enemy");
Debug.Log(enemies.length);
}
Your answer
Follow this Question
Related Questions
JS to C# converting issues.. 1 Answer
can anyone please help with enemy wave spawn? 0 Answers
Destroy and Spawn an Enemy 1 Answer
Trying to write enemy spawn in JS. 2 Answers
How to spawn objects in a specific range of random location 1 Answer