Need help to spawn random object from a list of game objects

Hello friend welcome . my code is as below

public GameObject[] Itemgroup1;

public List Itemgroup1list;

public GameObject Getitemgroup1() //function to be called

{

for (int i=0; i<Itemgroup1list.Count; i++) {
if(!Itemgroup1list*.activeInHierarchy)*

{
return Itemgroup1list*;*
}
}

this is my code that have a array of game object called “GameObject[] Itemgroup1” and each game object in that array is added to the list called “List Itemgroup1list” by some code that i didnt provided … so what i posted here is a function that is being called in game which returns a gameobject from the list which is currently inactive in heirarchy . So here is my problem while returning the inactive gameobject my function checks for an inactive game object in the list from top to bottom and returns the first gameobject that is found inactive and because of my game optimaizations there will only be 4 gameobjects active at anytime. so if my list contains 5 or more than 5 game objects , then only first 4 get active and in rare case the 5 th one also But the problem is that the remaining gameobjects in the list remains unactive whole the time . So what could i do so that that while returning any gameobject form my list my function returns a random inactive gameobject not the first inactive gameobject .
Hope the explanation of my problem was efficient…

Not a direct answer based on your code but this logic should get you on your way …

GameObject[] objs;
GameObject sel = null;
do
{
	sel = objs[Random.Range(0, objs.Length)];
} while (sel.activeSelf);

But be sure there will always be at least one inactive else this will result in endless loop. If that might be case you will want to build in some checks, like a counter that counts to say 10 and then force an exit of the loop via break (after setting sel = null so you can check that it was forced).