How to prevent same prefab from spawning twice in a row?

Hello everyone, Unity beginner here, I have a random prefab spawner attached to my game in Unity which randomly spawns 3 prefabs. The problem is, sometimes I get the same prefab like 5 times in a row. How can I prevent the same prefab from spawning twice in a row? Here is my code:

```
public class randomspawnscript : MonoBehaviour
{
    public GameObject prefab1, prefab2, prefab3;
 

    public float spawnRate = 2f;

    float nextSpawn = 0f;

    int whatToSpawn;

    void Update()
    {
        if (collisionbutton.end != true || gameoverscreenrestart.restartPressed==true || gameovermainmenu.menuPressed==true)
        {
            if (Time.time > nextSpawn)
            {
                whatToSpawn = Random.Range(1, 4);
                Debug.Log(whatToSpawn);

                switch (whatToSpawn)
                {
                    case 1:
                        Instantiate(prefab1, transform.position, Quaternion.identity);
                        break;
                    case 2:
                        Instantiate(prefab2, transform.position, Quaternion.identity);
                        break;
                    case 3:
                        Instantiate(prefab3, transform.position, Quaternion.identity);
                        break;
                }
                nextSpawn = Time.time + spawnRate;
            }
        }
        else
        {
            return;
        }
    }
}
```

Had similar problem and solved it with additional array of values. In your case that would be array[1,2,3]. Do random range on 0 to number of items in array. every time you get case remove that element from array. after switch check if array is length 0 (or 1 if that suits you) and recreate same array. Hope this helps without actual code.
If someone has better idea please post it.

If you want it to be random, but never have the same 2 in a row, you only need to keep track of which one was last, and choose a new index if it’s the same as last time. You can also make this more easily scalable by using an array of prefabs and get one using an index, instead of creating numbered variables and using a switch block.

public class randomspawnscript : MonoBehaviour
{
    public GameObject[] prefabs;

    public float spawnRate = 2f;
    float nextSpawn = 0f;
    int whatToSpawn;
    int lastSpawned = -1;
    void Update()
    {
        if (collisionbutton.end != true || gameoverscreenrestart.restartPressed==true || gameovermainmenu.menuPressed==true)
        {
            if (Time.time > nextSpawn)
            {
                nextSpawn = Time.time + spawnRate;

                // if there are no prefabs, do nothing
                if(prefabs.Length == 0)
                {
                    Debug.Log("No prefabs in array");
                    return;
                }

                // keep randomly selecting an index if it comes up the same as last time
                do
                {
                    whatToSpawn = Random.Range(0, prefabs.Length);
                }
                while(whatToSpawn == lastSpawned);
              
                Debug.Log(whatToSpawn);

                Instantiate(prefabs[whatToSpawn], transform.position, Quaternion.identity);

                lastSpawned = whatToSpawn;
            }
        }
        else
        {
            return;
        }
    }
}
2 Likes
[SerializeField] private GameObject[] prefabsToSpawn;
// In the inspector you will set you prefabs here

[SerializeField] private Transform spawnZone;
// Your zone where you're going to instantiate the prefab

private int randomIndex;
private int lastIndex;

void Start()
{
randomIndex = Random.Range(0, prefabsToSpawn.Length);
lastIndex = randomIndex;
Instantiate(prefabsToSpawn[randomIndex], spawnZone.position, Quaternion.Identity);
}

void Update()
{
SpawnNewPrefab()
// Will non stop generate prefabs but never 2 of same in a row
}

void SpawnNewPrefab()
{
randomIndex = Random.Range(0, prefabsToSpawn.Length);
if(randomIndex == lastIndex)
{
while(randomIndex == lastIndex)
{
randomIndex = Random.Range(0, prefabsToSpawn.Length);
}
}
lastIndex = randomIndex;
Instantiate(prefabsToSpawn[randomIndex], spawnZone.position, Quaternion.Identity);
}