Enemys adding to array then filling in Waypoints

Hello I wondered if anyone can offer some guidance on how to create a way that Objects/Enemy’s fill in waypoints or just spawn points. what I am after is if Enemy 1 goes in to Spawn 1 then 2 go to 2 etc… but if one is destroyed the next spawn goes into that.

I am using
enemy= GameObject.FindGameObjectsWithTag (“Enemy”); to add them to array and
agent.SetDestination(object.transform.position); to set the position.

was thinking trying if statement so if Enemy is within distance set var to true (not sure how to get the enemy due to there be more than 1) and having each location as a boolean.

var locationFull = false;
var inLocation : Float =2;
var dist = Vector3.Distance(Target.position, transform.position);

if(enemy < inLocation)
{
locationFull = true;
}

I have not written a script cause I am not sure how to go about doing this. (not after someone writing it for me just after help or direction how this could be done)

Many thanks for any help given.

Dean

The null check for game object is overloaded, so that if you have a reference to a game object, and that game object is destroyed, it will be null.

So to solve your problem, you will need an array of game objects that maps one-to-one to the spawn positions. Each time when you spawn, you will check each array entry starting at 0. You will use the first null entry in the array. If nothing is destroyed, then the array just continues to fill. If an enemy is destroyed, then this array scan will find the position.

Hi Dean,

was thinking trying if statement so if Enemy is within distance set var to true (not sure how to get the enemy due to there be more than 1) and having each location as a boolean.

One effective to do this would be to iterate through all of the enemy gameobjects and calculate the distance in your game update.

I’m not sure what you mean by ‘set var to true’, but a possible implementation for this would be to store the gameobjects into an ArrayList.

So, it would be something like this:

//Pseudocode
foreach enemyGO in enemies{
if(within range && (enemyGO is not in enemyList)){
enemyList.Add(enemyGO);}
}

Then, once you need to remove, you can simply do:
enemyList.Remove(enemyGO);

Regarding your spawning question, you could do this by iterating through the spawnAreas array when spawning enemies.

For instance, with a spawnAreas GameObject ArrayList, you could do the following:

//Pseudocode
foreach spawnArea in spawnAreas{
if(spawnArea is null && (enemyGO isn’t in spawnAreas)){
spawnArea = enemyGO;}
}

Then, prior to the enemy gameobject being destroyed, you would remove it from the spawnAreas:
spawnAreas.Remove(enemyGO);

This is all using ArrayList, but the same could be done using a regular GameObject array.

Hope that helps.

Regards,
Ezro