Quantcast
Channel: Latest Questions by ShadyProductions
Viewing all articles
Browse latest Browse all 31

Problems with removing a random from a list

$
0
0
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 availableJobs = new List(); 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[i] == _detach) { //loop through to find the one toReturn = availableJobs[i]; availableJobs.Remove(availableJobs[i]); //remove from list print("removing: " +availableJobs[i]); //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 ![alt text][1] [1]: /storage/temp/51343-screenshot-1.png

Viewing all articles
Browse latest Browse all 31

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>