List or Arrays, and how to randomize them.

I’m currently in a pickle, and I think writing it out and getting some advice is what I need. I have a pretty specific question it seems, and I only am more discouraged when trying to find answers, so here it goes-

The goal of the script that I want to create is a list or array of gameobjects ( or strings) that I can then use to randomly decide which order they will activate and when. For example, the player has 3 tasks they need to complete, task 1, task 2, task 3. I want to randomly pick which order the tasks come in. This way the player is not doing the taks in the same order.

I haven’t worked with arrays or lists to know if I am able to do this. I was hoping for some general guidance on how to structure this. I don’t have any code specific to this, but I would appreciate any guidance.

My thoughts were that I would be able to store the tasks as strings or gameobjects and have the script select them and store the order in variables, then when I need to reshuffle the tasks, overwrite those variables. However, I’m not sure how to put that in practice.
Thanks!

Good day.

You want to pick random elements from an array, correct?

Then you need to do 2 things, one, learn to pick a random elemnt, and learn how to check if that element was already picked.

you have this array of 5 Gameobjects:

GameObject[] MyArray = new GameObject[5]

If i want the 1st element of the array (index = 0)i do dins

MyArray[0]

The 3rd element;

Myarray[2]

So, if i want a random element, i need to do this:

int RandomNumber = Random.Range(0,MyArray.Lenght)
MyArray[RandomNumber]

So you will get a random element from ( 0 ) to (lenght of the array).

This is the 1st part.

As Random.Range doesn’t know what elements are already selected, you need to check if the Random.Range is selecting a neew element.

So you should create a bool array, to know if a element has been selected.

bool[] ConfirmationArray = new bool [MyArray.Lenght];

Every time, and element is selected, i will change to true the bool variable in the confirmation array.

int RandomNumber = Random.Range(0,MyArray.Lenght)
while (ConfirmationArray[RandomNumber] == true)
{ 
RandomNumber = Random.Range(0,MyArray.Lenght)
} // this while will make a randim number until it is a "new" number

something = MyArray[RandomNumber]
ConfirmationArray[RandomNumber] = true; // So it will not be picked again

If didnt understand something, goread some manual, API apges, youtube, etc…

Bye!

… And when random returns the array size, you got an error :slight_smile: