- Home /
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.
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.
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)
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.
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
Follow this Question
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