- Home /
Spawn Limit for enemies
Can someone please give me a script that will limit the enemies being spawned to 50 so i will have 50 enemies on my screen and if i have 49 then one will spawn. I already have a script that spawns enemies i only need a script that kills them and limits them to only 50 on 1 screen.
Answer by s4vi0r · Dec 29, 2010 at 07:13 PM
(=
All you really have to do is in your enemy script when you make then enemy die based on health or whatever spawn another.
Update() { if(health <= 0) { enemycount--; SpawnNewEnemy(); Destroy(this.gameObject); enemycount++; }
}
If you want to limit to 50 and you are managing them all from 1 script then you might want to either have a counter or store them in an array. Keep it under 50 by checking the size vs 50.
I would do it via array for management purposes.
var enemies : Array; var enemyPrefab : GameObject;
function Start() { enemies = new Array(); } function SpawnNewEnemy() { if(enemies.length >= 50) { return; }
var newEnemy = Instantiate(enemyPrefab, Vector3(0,0,0), Quaternion.identity); enemies.Add(newEnemy);
}
Thats helpful, but not in my case, I need something that wont let more then 50 spawn but what your script does is if one dies then another one will be born i only want something that will only spawn 50 enemies. Sorry if this is annoying but i have to do finish this because christmas holiday is almost over and i will have LOTS of tests so i need to finish everthing this week or wait a month and i want to submit my project to a contest. Oh and something to help you, my enemy and my player share the same script