How to prevent multiple spawns from a single spawn point?

Hey everyone,

I’m working on an endless runner style game where the player has to dodge blocks while collecting coins. I’m using four spawn points to spawn three blocks and one coin using the code below:

void SpawnBlocks()
    {
        int randomIndex = Random.Range(0, spawnPoints.Length);

        for (int i = 0; i < spawnPoints.Length; i++)
        {
            if (randomIndex != i)
            {

                Instantiate(blockPrefab, spawnPoints*.position, Quaternion.identity);*

Instantiate(coinPrefab, spawnPoints[randomIndex].position, Quaternion.identity);

}

}
}
Initially, I started the project by randomly spawning three blocks among four spawn points, so there would always be a gap for the player to go through. To make the game more engaging, I decided to spawn a collectable coin within the gap using whichever spawn point is not being used to spawn a block.
-
According to this script, i = all of the spawn points that are being activated in each instance. I had to figure out which spawn point was not being activated so that I could use that spawn point to spawn a coin. After simply realizing that the randomIndex != i, I figured out that I could use spawnPoints[randomIndex].position to spawn the coin because the randomIndex number is the number that is never being used for the block spawn points.
-
This worked flawlessly, or so I thought. When I tried implementing the score system, which would +1 for every coin that the player collected, I noticed that each time the player collected a coin, the score would +3 instead. That’s when I realized that, instead of spawning just one coin, I was spawning three coins on top of each other.
-
I would like to prevent the spawn point from spawning three coins at once and, instead, just spawn one coin, but I can’t wrap my head around how to do that with my current script.
-
I’ve also seen people utilize a check function to see if an object is already in the location before spawning another, which would work if these coins weren’t spawning at the exact same time.
-
My last idea is to change the score value from +1 per coin to +0.3 per coin, and then round the 0.9 from the stacked coins into a 1. This might be my best option, but I can’t quite figure out how to add the decimals together and then round to a whole number (I’m basically one DNA molecule away from a chimp, after all).
-
Apologies for the reading assignment, I thought it’d be best to fully explain my situation to receive proper assistance. If anybody knows of a solution/workaround then I would greatly appreciate it!

What you could do is create a pool and then remove the last used spawn point. Here’s an example:

public void SpawnBlocks() {

        // Create a pool
        List<Vector3> pool = new List<Vector3>();
        foreach (Vector3 v in spawnPoints) { pool.Add(v); }

        for (int i = 0; i < spawnPoints.Length-1; i++) {
            int randomIndex = Random.Range(0, pool.Count);

            Instantiate(blockPrefab, pool[randomIndex], Quaternion.identity);

            pool.RemoveAt(randomIndex);
        }

        // Now add the coin with the last remaining spawn point in the pool
        Instantiate(coinPrefab, pool[0], Quaternion.identity);
    }