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 MrFodds · Apr 05, 2014 at 08:08 AM · c#arraysrandom.range

Randomly Select String from Array

I've got a list of strings contained within a public void, yet any method I use to randomly select one of the strings does not work. Here's the code:

 public void rng () {
         ArrayList word = new ArrayList();
         word.Add("One");
         word.Add("Two");
         word.Add("Three");
         word.Add("Four");
         word.Add("Five");
         word.Add("Six");
         word.Add("Seven");
         word.Add("Eight");
         word.Add("Nine");
         word.Add("Ten");
     }
 
     void Start () {
         ans = rng[Random.Range(0, rng.Length)];
 
     }

Does anyone have a way I can fix this so the code will assign a string from the list to the variable "ans"? Thanks in advance, -MrFodds

Comment
Add comment · Show 10
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 AlucardJay · Apr 05, 2014 at 08:12 AM 1
Share

word needs to be a global variable. Call the function to populate the array. Then pick a random position in the array word :

 public void rng () {
     word = new ArrayList();
     word.Add("One");
     word.Add("Two");
     word.Add("Three");
     word.Add("Four");
     word.Add("Five");
     word.Add("Six");
     word.Add("Seven");
     word.Add("Eight");
     word.Add("Nine");
     word.Add("Ten");
 }

 ArrayList word;
  
 void Start () {
     rng();
     ans = word[Random.Range(0, word.Length)];
 }
avatar image AlucardJay · Apr 05, 2014 at 08:18 AM 1
Share

I edited the comment. word needs to be a global variable.

avatar image AlucardJay · Apr 05, 2014 at 08:29 AM 1
Share

I don't use ArrayLists, but this is a great reference : http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?#ArrayLists

So use word.Count

avatar image Hoeloe · Apr 05, 2014 at 08:47 AM 1
Share

It's not. It's because ArrayList converts everything you put in it to the base type object. To use anything from it, you have to cast it to the correct type. This is the major reason you don't want to use ArrayList, and use generic lists ins$$anonymous$$d.

I'd always recommend using a generic list over ArrayList (that's List. They're much safer to use, since they have an associated type.

avatar image Hoeloe · Apr 05, 2014 at 08:54 AM 2
Share

You need to use generic tags. C#'s built-in List class allows you to define a type for the list, which means you don't need to cast anything, and allows you to keep the typing of your objects while they're in the list. I unfortunately can't directly post an example because the comment system removes anything inside triangular brackets.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Random Number Generated within Array 2 Answers

Distribute terrain in zones 3 Answers

Obtaining the Variable of a Prefab in an array. 1 Answer

How do i convert this code snippet to a list? 1 Answer


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