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 RmurilloM · Apr 07, 2012 at 03:38 AM · arrayrandomstring

How to randomize an array of Strings?

Hi there!

I've found for a long time and I didn't find anything!

What I need is to pick up randomly a "name" from an array:

var NameArray : String[];

 NameArray[0] = "Name";
 NameArray[1] = "Name";
 NameArray[2] = "Name";
 NameArray[3] = "Name";

How can I pick a name randomly from that array? Please, help!!

Comment
Add comment · Show 2
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 RmurilloM · Apr 07, 2012 at 07:44 AM 0
Share

Thanks so much guys!

It was perfect!

avatar image AlucardJay · Apr 07, 2012 at 07:53 AM 0
Share

happy to help =]

just some tips on using this 'site (for ALL new users, not just @Rmurillo$$anonymous$$ ) :

post comments to answers by clicking "add new comment" , a window then opens to type in. (you can delete your comments too, so please delete the self-answer-comment you put on this question).

Also to mark a correct answer for future readers, click the 'Tick' under the 'Thumb' next to the answer. (even though I feel rewarded enough by your nice feedback).

Otherwise, no problem. Happy Coding =]

2 Replies

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

Answer by AlucardJay · Apr 07, 2012 at 03:59 AM

chosenName = NameArray[Random.Range(0, NameArray.length)];

EDIT :

Are you sure ?

make a new scene and a new script. attach the script to the camera, then copy and paste this into the script :

 #pragma strict
 
 var NameArray : String[] = ["Name 00", "Name 11", "Name 22", "Name 33"];
 var chosenName : String;
 
 function ChooseName() 
 {
     chosenName = NameArray[Random.Range(0, NameArray.length)];    
     Debug.Log("chosenName " + chosenName);
 }
 
 function OnGUI() 
 {
     if (GUI.Button(Rect(10, 10, 100, 35), "random")) {ChooseName() ;}
     GUI.Box (Rect(120, 10, 100, 35), "" + chosenName);
 }
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 Berenger · Apr 07, 2012 at 06:07 AM 0
Share

That code is correct. If you still have a problem, something else is wrong.

avatar image Kleptomaniac · Apr 07, 2012 at 07:35 AM 0
Share

@Rmurillo$$anonymous$$ I believe you may have misunderstood what @alucardj is specifying here ... yes, the output of the Random.Range() will be an int, however that int is then used to deter$$anonymous$$e a specific element of your chosenName array and this string is outputted. :) So, as @Berenger said, that code is most definitely correct. :D

avatar image fafase · Apr 07, 2012 at 08:33 AM 0
Share

I get confused with this Random.Range($$anonymous$$,max); the documentation says both $$anonymous$$ and max are inclusive, but I realized that the max is never given. Also, in other languages, the max is exclusive. So is the documentation wrong? I guess so. If the doc is right then the code needs to be NameArray[Random.Range(0, NameArray.length-1)];

avatar image Kleptomaniac · Apr 07, 2012 at 09:30 AM 0
Share

You're correct. :) Trying to specify an element of an array with its .length value will cause an "Array index is out of range" because of the inclusion of an Element 0. So, yes, @alucardj, it should be NameArray[Random.Range(0, NameArray.length - 1)]; :P

EDIT

Sorry, my statement is incorrect. See below.

avatar image AlucardJay · Apr 07, 2012 at 02:05 PM 0
Share

@fafase Are you Sure ?!

Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).

http://unity3d.com/support/documentation/ScriptReference/Random.Range.html

Interesting, as I hadn't read this, only went from experience. So ya made me check, @$$anonymous$$leptomaniac :P . If you want to confirm test yourself, in the posted script delete the OnGUI function and change the ChooseName function to Update. It never returns an "Array index is out of range" . =]

Show more comments
avatar image
0

Answer by RmurilloM · Apr 07, 2012 at 04:23 AM

Thanks for your answer!

I already did that, but chosenName is going to be a int. And what I need is the String that number has in the Array!

Thanks again!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Referencing private string for array name 2 Answers

Assigning array element to variable 1 Answer

Setting variable to random string from an array? 1 Answer

String Access problem 1 Answer

how to randomly pick a string from an array 3 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