x


Is it possible to grab an index in a List by random as in arrays?

I'm using a list to find and store a gameobjects child transforms in. I use a list mainly because I don't want all childs of the go, only the ones taged "Detachable" and for some go's that use the script they are quite a few so would be a pain adding them in the inspector.

Anyways, I now want to add the code to pick a random index from the list and add it to a new object kinda like:

Transform _detach = _myArray[Random.Range(1, _myArray.Length);

Is it possible to do something similar to this with a List?

more ▼

asked Mar 02 '11 at 02:40 PM

Kona gravatar image

Kona
253 9 10 21

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

2 answers: sort voted first

Yes, you'd want something like:

List<Transform> _myList = ...;
Transform _detach = _myList[Random.Range( 0, _myList.Count )];

i.e. Lists have "Count" instead of "Length".

Also note that the lower bound on the Random Range is 0, not 1 (which should be the case in yours too).

And finally, note that the upper bound on Random Range is exclusive for the integer version (different than the float version) so the maximum it would return is Count - 1.

more ▼

answered Mar 02 '11 at 02:50 PM

Molix gravatar image

Molix
4.8k 17 27 66

Good point on the lower bound being inclusive. I assumed the OP intended to keep the first child un-detached for whatever reason.

Mar 02 '11 at 02:52 PM burnumd

Thanks :) ah, so basicly there's always one object in the list that can't be reached when picking by random? And thanks for quick answer btw

Mar 02 '11 at 03:03 PM Kona

No, that's not what Molix is saying. It means that if you want the first thing in the list, you need to use index 0 (the same is true for Arrays). The description for Random.Range ( http://unity3d.com/support/documentation/ScriptReference/Random.Range.html ) talks about what it means for min and max to be inclusive or exclusive.

Mar 02 '11 at 03:14 PM burnumd
(comments are locked)
10|3000 characters needed characters left

Change _myArray.Length to _myArray.Count. See the documentation for List here.

Also, you should probably change your variable name from _myArray to something that makes sense for what you're using it for (note: _myList isn't really any more descriptive) so we have an idea that you've actually declared a List and not an array. Even more helpful, posting the error message you receive from code you've posted (in this case it would be "'Length' is not a member of System.Collections.Generic.List" or some such) would help people diagnose problems more easily.

more ▼

answered Mar 02 '11 at 02:51 PM

burnumd gravatar image

burnumd
3.4k 22 35 71

(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:

x599
x382
x111
x66

asked: Mar 02 '11 at 02:40 PM

Seen: 1061 times

Last Updated: Mar 02 '11 at 02:40 PM