Problems with removing a random from a list

I am trying to cycle through a list of enum variables
and return a random one from it, then remove it from the list
however some of them return index out of bounds and I can’t get a workaround it
can anyone help me? - Regards, Shady

public enum Jobs {miner, builder, blacksmith, cook, None};
public List<Jobs> availableJobs = new List<Jobs>();
private Jobs toReturn;
private Jobs _detach
		
void Start() {
    foreach (Jobs jobs in Enum.GetValues(typeof(Jobs))) { //get all jobs into list
        	availableJobs.Add(jobs);
    }
}

public Jobs lookForAvailableJob() { //function to find a job
	if(availableJobs.Count == 0) { //if list is empty
		availableJobs.Clear();
		return Jobs.None; //default
	} else { //if not
		_detach = availableJobs[Random.Range( 0, availableJobs.Count )]; //get a random
		for (int i=0; i < availableJobs.Count; i++) {
			if (availableJobs *== _detach) { //loop through to find the one*

_ toReturn = availableJobs*;_
_ availableJobs.Remove(availableJobs); //remove from list*
print("removing: " +availableJobs*); //checking*
* }
}
}
return toReturn;
}
void Update () {
if (lookForAvailableJob() == Jobs.None) {
print(“null”);
} else {
lookForAvailableJob();
//print(lookForAvailableJob());
}
}
I’m getting these kinds of outputs in the console
[51343-screenshot-1.png*
|51343]*

_*

You lookForAvailableJob function needs some modification. Since you are removing an element from an array on which you are looping through. It is not advice-able to do it like this. Instead you can capture the element then either break the loop and delete it, or, delete it when it comes out of the loop. The one i am suggesting below does not force to implement loop.

 public Jobs lookForAvailableJob() { //function to find a job
     if(availableJobs.Count == 0) { //if list is empty
         availableJobs.Clear();
         return Jobs.None; //default
     } else { //if not
         _detach = availableJobs[Random.Range( 0, availableJobs.Count )]; //get a random
         availableJobs.Remove(_detach); //remove from list
     }

     return _detach;
 }

Hi, i guess the problem is the line 22: you “print” the name of the element i just after removing it! If you remove the last element of the list you can’t print it’s value after removing it! Also, the text displayed will be wrong for other elements = you remove element i but will display the name of element i+1 (because you just removed element i)

Also, no need to continue to iterate after you removed the element. A “break” statement would be fine here.