- Home /
Enemy spawner help with lengths
How can I get a count of child gameobjects by name (NOT BY TAG)? I made an enemy spawner that first looks for and counts tagged gameobjects named "ReSpawn", but then I need it to find and get a count of children in all those ReSpawns it finds with a specific name (Not Tag).
I am trying to construct a script for multiple enemy spawners. For each spawner I can drag and drop any type of enemy into a 'gameobject[]' string. Each spawner will first count how many enemies are active in the level by looking for a tag, and then if the enemy.length is below (x), it will(the idea, the part I need help with) check if the active enemies have a child with a name I manually enter through the inspector for each Spawner I place in the level.
This way I can place spawners in any area in a level, and it will spawn only the enemies I've put in its string, when its specific enemy count is down.
I hope my script is easy enough to catch the drift of my goal
var ThisSpot : String; // what to name instantiates for this spawners area
var Oppos : GameObject []; //drag and drop enemies I choose to intantiate here
var OppoCount : GameObject [];
var HowMany = 3; // how many enemies to spawn from this spawner
var spawnSizeArea = 25;
function Update () {
OppoCount = GameObject.FindGameObjectsWithTag("Respawn");
for ( ops in OppoCount){
ops = transform.Find(ThisSpot).gameObject; // This is the problem. How do I correctly
//get a count of child objects in 'ops' ??
print(ops.length);
if(ops.length < HowMany){ // if this specific spawners instantiates count is below 'HowMany', spawn more
StartCoroutine ("Spawn");
}
}
}
function Spawn(){
var length = Oppos.length-1;
var index = Mathf.Round(length*UnityEngine.Random.value);
curSpawn = Oppos[index]; // pick a random enemy to spawn from the 'Oppos' string each time
var xz = Random.insideUnitCircle * spawnSizeArea;
var newPosition = Vector3(xz.x,0,xz.x)+transform.position;
yield WaitForSeconds(Random.Range(70,120)); // how long to wait between spawns
var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation);
newOp.transform.Find("fighter").name = ThisSpot; //name child object in string so you can find this specific spawners instantiates
StopCoroutine("Spawn");
}
Your answer
Follow this Question
Related Questions
Shoot Bullet Delay Enemy 0 Answers
Spawning different random objects at the same position? 2 Answers
RPC , string parameter is sent, but length is 0 1 Answer
Enemy Wave 1 Answer
Spawn Limit for enemies 1 Answer