Spawn only a certain number of enemies over a certain time

OK I cobled together a simple "Alien Zombie" spawner using an empty game object and attaching a spawn script. I have about 3 of them strategically placed on my game terrain and they spawn every 200 seconds which after a lot of test playing I think is a fairly safe number to allow my players to explore the terrain and figure out where they need to go and what they need to do.

But just in case I'm wondering if there is a way to make this spawn ONLY a certain number of Alien Zombies over a certain time period.

Say something like after a maximum of 200 Zombie Aliens (Number to be tweaked based on ammo caches available on my terrain) STOP spawning them so that if my player runs out of ammo or takes longer to figure out where and what to do that they won't get OVERRUN with Alien Zombies.

I hope this makes sense, if not, let me know and I'll try explaining it in another way ;)

var alienZombiePrefab : GameObject;

InvokeRepeating("SpawnZombie" , 200, 200);

function SpawnZombie () {

        Instantiate(alienZombiePrefab, transform.position, transform.rotation);

}

EDIT

Almo this is what's in my Awake function in my AlienZombieAI script from what you have done bellow:

function Awake (){

    var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
    var countScript : ZombieCountScript countScript = tempobj.GetComponent(ZombieCountScript);
    countScript.m_numZombies++;

    var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
    var countScript : ZombieCountScript = tempobj.GetComponent(ZombieCountScript);
    countScript.m_numZombies--; 

}

I get an error for line 17: Assets/WeaponScripts/ZombieAlienAI.js(17,44): UCE0001: ';' expected. Insert a semicolon at the end.

Which is this line:

var countScript : ZombieCountScript countScript = tempobj.GetComponent(ZombieCountScript);

EDIT: *Almo*:

I changed my code to this in my AI script:

function Awake (){

    var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
    var countScript : ZombieCountScript = tempobj.GetComponent(ZombieCountScript);
    countScript.m_numZombies++;
}

Now I get the following errors: Error 1: Assets/Misc Scripts/ZombieCountScript.js(6,24): BCE0019: 'm_numZombies' is not a member of 'ZombieCountScript'.

Error 2 Assets/Misc Scripts/ZombieCountScript.js(8,29): BCE0005: Unknown identifier: 'alienZombiePrefab'.

Error 3 Assets/WeaponScripts/ZombieAlienAI.js(18,21): BCE0019: 'm_numZombies' is not a member of 'ZombieCountScript'.

????

My Zombie Counter Script:

function SpawnZombie () {

    var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
    var countScript : ZombieCountScript = tempobj.GetComponent(ZombieCountScript);

    if(countScript.m_numZombies < 200)
    {
        Instantiate(alienZombiePrefab, transform.position, transform.rotation);
    }
}

2 Answers

2

Still call InvokeRepeating on SpawnZombie, but add the stuff below to cap the total number.

Create a script called ZombieCountScript with an integer member m_numZombies and attach it to an object tagged "ZombieCounter".

function SpawnZombie () {

    var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
    var countScript : ZombieCountScript = tempobj.GetComponent(ZombieCountScript);

    if(countScript.m_numZombies < 200)
    {
        Instantiate(alienZombiePrefab, transform.position, transform.rotation);
    }
}

And in your Zombie Script's Awake() function

var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
var countScript : ZombieCountScript countScript = tempobj.GetComponent(ZombieCountScript);
countScript.m_numZombies++;

And wherever a Zombie is killed

var tempobj : GameObject = GameObject.FindWithTag("ZombieCounter");
var countScript : ZombieCountScript = tempobj.GetComponent(ZombieCountScript);
countScript.m_numZombies--;

There are some other bits, like initializing the zombiecount variable, but this should get you started.

Would the object tagged "ZombieCounter" be an empty game object that I could just toss in anywhere in my scene? And by Zombie Script do you mean my AI script for my Alien Zombies?

Yup, ZombieCounter is just an empty object to attach the counter script to for access from multiple spawners. Yes, ZombieScript is whatever script you use to run the zombies. If you want to stop spawning entirely once 200 is reached, then just do CancelInvoke calls on the spawners once there are 200. If you want 200 spawned, not 200 in existence, then don't do m_numZombies--.

Awesome almo I'll have to try this out ASAP :D

I followed your instruction and it's giving me a load of errors?

I got another way of doing it:

        var alienZombiePrefab : GameObject;
var zombieNumber : int = 0;
    if (zombieNumber < 201){
InvokeRepeating("SpawnZombie" , 200, 200);
}
    function SpawnZombie () {
            Instantiate(alienZombiePrefab, transform.position, transform.rotation);
zombieNumber++;
    }

I haven't checked it or anything, but I'd do something along those lines.

Once InvokeRepeating is called, it will keep calling SpawnZombie. You need a way to get it to stop. He's also got multiple spawners, so they all need to know how many zombies there are.

His question says "maximum 200 zombies", so I assumed he meant in total. And, to my knowledge, once you say "InvokeRepeating" it keeps going until you do a CancelInvoke call. My setup has an additional benefit that it allows more zombies to spawn once the player starts killing them.

Naw I just for arguments sake wanted there to at any given time be LIMITED to a MAX count of 200 Alien Zombies total number to be spawned and then at the end of reaching 200 to STOP spawning them... hope that made sense ;)