Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kona · Mar 02, 2011 at 02:40 PM · listrandomrangeindex

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by Molix · Mar 02, 2011 at 02:50 PM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burnumd · Mar 02, 2011 at 02:52 PM 0
Share

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

avatar image Kona · Mar 02, 2011 at 03:03 PM 0
Share

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

avatar image burnumd · Mar 02, 2011 at 03:14 PM 0
Share

No, that's not what $$anonymous$$olix 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 $$anonymous$$ and max to be inclusive or exclusive.

avatar image nrubin29 · Jul 06, 2014 at 04:48 PM 0
Share

I would upvote if I could. Thanks!

avatar image
0

Answer by burnumd · Mar 02, 2011 at 02:51 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

How can I display 4 items in random order but never double? 2 Answers

Problems with removing a random from a list 2 Answers

Find the correct index number for array inside two loops 2 Answers

Random object, Array index out of range 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges