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 Jsyoung43 · Nov 19, 2013 at 08:59 PM · listrandomstringlabeloutput

How do I get a list to randomly select....

How do I get a list to randomly select and output a string? I am trying to make a List that has my Answer Strings randomly select an answer and output to the label for the button. I have not used lists in quite awhile so I an drawing a blank

here is some of the code to help


The Button

 if(GUI.Button(buttonSizeA, "A"))    // make a button
         {
             print ("Button A");
             WaipointCounter = 4;
         }
     GUI.Label (AnswerSizeA, choices(0));



The List

 public void GetAnswer()
     {
         List<string> choices = new List<string>(4);
         choices.Add(AnswerStringA);
         choices.Add(AnswerStringB);
         choices.Add(AnswerStringC);
         choices.Add(AnswerStringD);
     }



Thank you for your time and help in advance.

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

1 Reply

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

Answer by Cherno · Nov 19, 2013 at 09:07 PM

 RandomString = choices[Random.Range(0, Mathf.Round(choices.Count - 1))];
Comment
Add comment · Show 6 · 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 Jsyoung43 · Nov 19, 2013 at 10:42 PM 0
Share

how would I add it to the button so that the string would show up and how can I keep it from repeating

avatar image Cherno · Nov 19, 2013 at 11:00 PM 0
Share

No repeating:

 choices.Remove(RandomString);

You can show the string just as any other GUI.Label.

avatar image Cherno · Nov 19, 2013 at 11:13 PM 0
Share

No. choices.Remove(RandomString); is supposed to go after assigning the RandomString; It removes this string from the list so it won't get chosen again next time.

In the GUI.Label code, you would just use RandomString, which is the variable in which your randomly chosen string is stored.

avatar image WilliamLeu · Nov 19, 2013 at 11:18 PM 0
Share

The documentation says Random.Range is max exclusive for integers, so it should be choices[Random.Range(0,choices.Count)]. Also I don't think you would need the $$anonymous$$athf.Round if you're just dealing with all integers.

avatar image Cherno · Nov 19, 2013 at 11:22 PM 0
Share

WilliamLeu:

Ah yes I forgot that Random.Range also handles integers. In this case, the $$anonymous$$athf.Round is indeed not needed:

 RandomString = choices[Random.Range(0, choices.Count - 1)];

Jsyoung:

Where is your problem? you have the string, just insert it in your GUI.Label code and it will work.

If you are not sure about the syntax of how GUI.Label, refer to the Unity3D user reference.

http://docs.unity3d.com/Documentation/ScriptReference/GUI.Label.html

 GUI.Label(YourLabelPosition, RandomString);
Show more comments

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

19 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

Related Questions

(c#) display a random string at game over 2 Answers

Random role assigning from string array 1 Answer

A node in a childnode? 1 Answer

Trouble displaying lists of strings in a text box 1 Answer

How to Detect Touch on a Random String from 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