In my spawn script, how do I prevent the next enemy spawn from being the same as the last spawned?

It’s been awhile since I worked on my game and I am stuck. I have my spawn script that has an array of enemy prefabs. I want it so the next spawned enemy will be different than the last. I don’t want back to back spawns of the same enemy.

Any help on how to do this? Here is a snippet of the script I currently have:

public GameObject[] enemies;

IEnumerator SpawnObject(int index, float seconds)
{

    yield return new WaitForSeconds(seconds);
    Instantiate(enemies[index], enemies[index].transform.position, enemies[index].transform.rotation);
    //SimplePool.Spawn(enemies[index], enemies[index].transform.position, enemies[index].transform.rotation);

    isSpawning = false;
}

void Update()
{

    score = ScoringFarmStory.scoreFarmStory;
    isDead = PlayerFarm.isDead;


    if (!isSpawning && isDead == 0 && score >= 70)
    {
        isSpawning = true;
        int enemyIndex = Random.Range(0, enemies.Length);
        StartCoroutine(SpawnObject(enemyIndex, 0.28f));
    }

Any help would be appreciated, thank you!

You could always do the following:

int lastSpawn := last spawned id
int enemyTypeCount := count of enemy types

int id = Random.Range(0, enemyTypeCount-1)
if (id == lastSpawn)
    index++
lastSpawn = id

This makes sure, that every enemy has the same spawnchance, excpet for the one spawned last
There’s only one problem, that whatever value you set the lastSpawn, that monster can’t be spawned first
(You could solve this by setting it to -1 then in the Random.Range:
0, enemyTypeCount - (lastSpawn == -1 ? 0 : 1)

Note that using random.range as an int does not require you to subtract 1. If you subtract 1, you’ll actually not spawn all groups as the int version is not inclusive on the last number.

https://docs.unity3d.com/ScriptReference/Random.Range.html

I messed up a bit btw, it should be if (id >= lastSpawn)
So you didn’t understand, huh?
it’s meant to leave a gap like so:
possible values 0-5
I generate values from 0-4
if the value is greater than lastspawn, I add one to it, so I get the following possible results: (lastSpawn=3)
0, 1, 2, 3+1, 4+1