Choosing a random available spawnpoint

Hello, I’m currently working on a 2 player local multiplayer capture the flag game. On a map are 6 spawnpoints, point 1 till 6. Each spawnpoint can spawn a variety of items, all stored in an array. Every 20 seconds, a coroutine checks if there are 6 items in the scene. If this is not the case, new items are spawned until the itemcount is 6. If a spawnpoint is available or not is stored in a boolean array.

However, the code I’m currently using causes the spawnpoints to be checked in a specific order, from left to right. I want the objects to spawn at a random spawnpoint, which is available (false bool in array).
Otherwise, you can predict where a new item will be spawned. How should I do this?

IEnumerator NewItems()
    {
        float timer = 20f;


        while (timer >= 0f)

        {
            timer -= Time.deltaTime;
            yield return null;
        }
        

        for (int i = 6; i > ItemCount;)
        {
            if (SpawnPoints[0] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point1.position, Point1.rotation);
                SpawnPoints[0] = true;
                ItemCount++;
            }
            else if (SpawnPoints[1] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point2.position, Point2.rotation);
                SpawnPoints[1] = true;
                ItemCount++;
            }
            else if (SpawnPoints[2] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point3.position, Point3.rotation);
                SpawnPoints[2] = true;
                ItemCount++;
            }
            else if (SpawnPoints[2] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point3.position, Point3.rotation);
                SpawnPoints[2] = true;
                ItemCount++;
            }
            else if (SpawnPoints[3] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point4.position, Point4.rotation);
                SpawnPoints[3] = true;
                ItemCount++;
            }
            else if (SpawnPoints[4] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point5.position, Point5.rotation);
                SpawnPoints[4] = true;
                ItemCount++;
            }
            else if (SpawnPoints[5] == false)
            {
                int randomItem = Random.Range(0, 5);
                Instantiate(randomLevelStartItems[randomItem], Point6.position, Point6.rotation);
                SpawnPoints[5] = true;
                ItemCount++;
            }
        }

Hello.

As there are only 6 spawns…

If you create a bool array of 6 length (1 for each spawn), call it SpawnsUsed, and change the value when the spawn is used and when its available.

Then only need to do this (ofc is not code, just the logic):

RandomNumber = Random (1 to 6)

While (SpawnsUsed[RandomNumber] = true)
{
RandomNumber = Random (1-6)
}

This will leave the while only with an available spawnpoint chosed at random.

Now only need to spawn there the item, and change its SpawnUsed value to true

Do not forget to change the bool value to false once a player take an item!

Bye!