How to randomize the order of objects?

Hi, so I have a situation set up where 10 different unique objects will enter from different directions and every time you press Spacebar, an object gets SetActive(false). My goal is to randomize the order successfully. And I have been able to randomize it. But the main problem is that even when lets say Object 5 gets SetActive(false), the randomization will still come back to trying to repeat Object 5 when it should not be selected anymore. Is there a better/simpler way to achieve this?

.

Any help would be appreciated!

.

public void RandomObjectFunction()
{
    randObject = Random.Range (1, 10);

	Objects [randObject].GetComponent<Animator> ().enabled = true;

	MasterBool = true;
}

@ILoveMyDada
Try this:

int objectsDisabled = 0;
public void RandomObjectDisable()
{
    if (objectsDisabled < Objects.Length)
    {
        int randObject = Random.Range(0, Objects.Length - objectsDisabled);
        GameObject selectedObject = Objects[randObject];
        Objects[randObject].SetActive(false);
        for (int i = randObject; i < Objects.Length - 1; i++)
        {
            Objects *= Objects[i + 1];*

}
Objects[Objects.Length - 1] = selectedObject;
objectsDisabled++;
}
}

public void RandomObjectFunction()
{
// Make sure to run RandomObjectFunctionfunction only 10 times or it will hang
while (true) {
randObject = Random.Range (1, 10);
if (Objects [randObject].GetComponent ().enabled == false) {
Objects [randObject].GetComponent ().enabled = true;
break;
}
}
MasterBool = true;
}

You could reassign the position of the deactivated gameobject to be the last in the array of random objects, and instead of having Random.Range(1, 10) you could replace 10 with a variable int that is deducted from each time the function is called, effectively giving you a smaller pool that no longer includes the deactivated objects.
You could also create a list of game objects and add the deactivated ones to it as you go. Then in the randomization function check if the selected object is on that list, and if so, run the function again until one not on the list is selected.

Make sure you have a list of references that’s only used for this purpose, shuffle the list once, and just pop from it when you want an object to deactivate. No worrying about tracking used indices; pop until the list is empty.

There’s good discussion on C# options for randomizing a list in this StackOverflow thread (and lots of other places, I’m sure, since this may or may not be entirely up to date). I’m not suggesting a specific one myself because it’s worth reading the thread regarding performance characteristics and differing implementations.

One way of simplifying some of the already proposed answers is to use the C# LINQ tool. With it, you can do something like the following:

using System.Linq;

var filteredList = Objects.Where(obj => obj.isActive).ToList();
randObject = Random.Range (1, filteredList.Count);
filteredList[randObject].GetComponent<Animator>().enabled = true;

Here are a couple resources with more information about LINQ. It’s a really neat tool for filtering and sorting data, definitely worth knowing about if you’re developing in C#!