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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Carnifex12 · May 17, 2014 at 04:34 AM · arrayrandomstring

Assigning array element to variable

So I have an array of some names:

 var firstNames = new Array ();
 
 firstNames = ["Brian","Paul","George","Jacob","Richard"];

I want to assign a random name from this array to some variables:

 var person1 = "";
 var person2 = "";
 var person3 = "";
 
 var firstNames = new Array ();
     
 firstNames = ["Brian","Paul","George","Jacob","Richard"];

From what I've found looking for an answer to this already, this is supposed to work:

 var person1 = randomFName;
 var person2 = randomFName;
 var person3 = randomFName;
     
 var firstNames = new Array ();
 var randomFName = "";
 
 function Start () {
 
 randomFirstName();
 
 }
 
 function randomFirstName() {
         
 firstNames = ["Brian","Paul","George","Jacob","Richard"];
 
 randomFName = firstNames[Random.Range(0,firstNames.Length)];
 
 }

Now this gives me a few errors and I know that I'm probably way off and this should be simple. I just want to be able to assign one of the names from to array to a variable but I want it to be a random name every time. To be clear, I don't want "randomFName" to be set as a name from the array and then every "person" variable to be set as that name, but for a random name to be selected for everyone. I realize it's still possible for each variable to be set as the same name anyways.

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

Answer by rgowen · May 17, 2014 at 05:19 AM

You should probably post the specific errors or exceptions that are being thrown, that might be a little more helpful.
However, if I understand correctly what you are trying to do, I'd probably implement it like this:

 var person1;
 var person2;
 var person3;
  
 var firstNames = new Array ();
  
 function Start () {
     firstNames = ["Brian","Paul","George","Jacob","Richard"];
     person1 = getRandomFirstName();
     person2 = getRandomFirstName();
     person2 = getRandomFirstName();
 }
  
 function getRandomFirstName() {
     return firstNames[Random.Range(0,firstNames.Length)];
 }

If you're getting an off-by-one issue (possibly IndexOutOfRangeException), you might have to use Random.Range(0,firstNames.Length-1), as the Length property returns the number of items in the array, which is equal to the index of the last item plus one.

Comment
Add comment · Show 3 · 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 Carnifex12 · May 17, 2014 at 09:48 PM 0
Share

Thanks for the reply, but I tried the code you posted and now I'm getting this error:

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion(IConvertible convertible)
avatar image rgowen · May 17, 2014 at 11:30 PM 0
Share

I'm not really sure what was wrong with it because I didn't get that error, but here is a corrected script that I tested and it works:

 #pragma strict
 
 public var person1 : String;
 public var person2 : String;
 public var person3 : String;
  
 var firstNames = new Array ();
  
 function Start () {
 firstNames = ["$$anonymous$$","Paul","George","Jacob","Richard"];
 person1 = getRandomFirstName();
 person2 = getRandomFirstName();
 person3 = getRandomFirstName();
 }
  
 function getRandomFirstName() {
 return firstNames[Random.Range(0,firstNames.length-1)];
 }

EDIT: Just realized that you're using Boo, I thought you were using Javascript so that's what I wrote the script in. I'd suggest using Javascript or C# over Boo as there's more support for it on Unity Answers and on the forums.

avatar image Carnifex12 · May 19, 2014 at 06:09 AM 0
Share

I figured it out, thanks for the help, I was getting that error because I used "Length" in the code ins$$anonymous$$d of "length"

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

21 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

Related Questions

how to randomly pick a string from an array 3 Answers

Referencing private string for array name 2 Answers

How to randomize an array of Strings? 2 Answers

Setting variable to random string from an array? 1 Answer

String Access problem 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