x


Making a List of objects and randomize the content

Hi every one, I got this little problem:

// How to make this
GameObject[] toChoose = {Object1, Object2, Object3, Object4... Object10};
// In to this
toChoose = {Object5, Object8, Object2, Object9... Object3};

I try using the Random function but does not work to well, any help will be appreciated. Thanks in advance.

more ▼

asked Jan 10 '12 at 05:48 PM

3D-Magic-VR gravatar image

3D-Magic-VR
41 12 14 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Do a google search for Fisher-Yates Shuffle. This should yield you plenty of results for randomizing an array.

more ▼

answered Jan 10 '12 at 05:53 PM

dannyskim gravatar image

dannyskim
3.9k 5 7 19

Thanks for the reply, your answer help a lot.

Jan 10 '12 at 10:46 PM 3D-Magic-VR
(comments are locked)
10|3000 characters needed characters left

I'd do something like this, it should theoretically go through each element and assign it somewhere else. Note that there is a possibility that an object does not move.

void RandomizeArray(GameObject[] inputs)
{
    for(int i=0;i<inputs.Length;i++)
    {
        GameObject buffer = inputs[i];
        inputs[Random.Range(0,inputs.Length)] = buffer;
    }
}
more ▼

answered Jan 10 '12 at 06:05 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

Your answer is not to solid, some times there's double or even triple numbers, but thanks anyway.

Jan 10 '12 at 10:48 PM 3D-Magic-VR
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x13
x4
x4
x3

asked: Jan 10 '12 at 05:48 PM

Seen: 676 times

Last Updated: Jan 10 '12 at 10:48 PM