Only instantiate GameObjects with a certain tag from an array

Hi. I’m trying to create a bit of code that instantiates only game objects from an array that are not already active in the game view. What I have at the moment is something that gives a tag to the spawned game object and I’m trying to exclude the ones with a Spawned tag from the random number generator’s choices. Here’s a some of the code:

    void SpawnPerson () {
            int randNum = RandomGen();
            Vector3 randomPosition = new Vector3
                    Random.Range (boundary.xMin, boundary.xMax),
                    Random.Range (boundary.yMin, boundary.yMax),
            if(CubeMover.keepClosed == false)
                GameObject person = Instantiate
                        as GameObject;
            personArray[randNum].tag = "Spawned";
        int RandomGen () {
            int i;
            i = Random.Range(0, personArray.Length);
            return i;

You can’t really do it by using the tag, because you would have to do something like a while loop and continue generating random indices into the personArray until you get to one that doesn’t have the tag “Spawned”, which would grow exponentially as you continued.

A solution to this problem that I’ve done myself is to create a List containing the index from 0 to PersonArray.Length-1 as list elements, like this:

List<int> possibleChoices = new List<int>(PersonArray.Length);
for (int i=0; i < PersonArray.Length; ++i)

Then you can random pick an element from this list:

int index = Random.Range(0, possibleChoices.Count);
PersonArray pa = personArray[index];
// Use pa

So possibleChoices gets reduced in size over time but always only contains the unspawned item indices into personArray. You will probably also want to check that possibleChoices.Count is greater than 0 and re-add all indices back to it when it hits count of 0.