Problems With List of Object in C#

if (GUI.Button (new Rect (Screen.width * 0.2875f, Screen.height - 35, Screen.width * 0.2f, 20), “Change Class”)) {

						GameObject Listing = GameObject.Find ("Troops");
						SControler = (TroopsLists)Listing.GetComponent (typeof(TroopsLists));

						foreach (GameObject unit in (SControler.troopers)) {

								Troopcontroler = (Troopclassecontroller)unit.GetComponent (typeof(Troopclassecontroller));
								Troopcontroler.actualclass = 1;
								Troopcontroler.classdetermination ();
						}
				}

I Need to take one random object from a list and change a variable from that object and I spent almost 2 hours search to a solution and nothing.

I have this GUI buttom that when I press it will change the variable from all objects from the list, and I just need one

Here you are changing the variable for all the objects. You need to use “static float Range(int min, int max)”. Use this code…

  if (GUI.Button (new Rect (Screen.width * 0.2875f, Screen.height - 35, Screen.width * 0.2f, 20), "Change Class")) 
{
   
 GameObject Listing = GameObject.Find ("Troops");
    SControler = (TroopsLists)Listing.GetComponent (typeof(TroopsLists));
     
    //If trooper is array then use "trooper.Length" if that is List then use "trooper.Count" in the following line.

     int iIndex = Random.Range(0 , SControler.troopers.Length);
    Troopcontroler = (Troopclassecontroller)SControler.troopers[iIndex].GetComponent (typeof(Troopclassecontroller));
    Troopcontroler.actualclass = 1;
    Troopcontroler.classdetermination ();
    
    }

From a list or from the array of object you have there?
Well, in both cases it goes the same:

int index = Random.Range(0, list.Count);
list[index].GetComponent<Script>().variable = value;

or

int index = Random.Range(0, array.Length);
array[index].GetComponent<Script>().variable = value;