- Home /
problem with random array
Hi,
For some reason that i cannot fathom for myself, or fix by searching, the below code does not seem to be getting an array from the 4 in the list. The text in the GUI Label works fine and i get no errors in the console when i play it, but after the line "Edison Utd Vs. " then it is just blank, and doesnt appear to be choosing a random name.
If anyone could explain this to me, without telling me to seach google then i would be very grateful. Everything i have found on google is alot more complex than this and doesnt really give me the answer i am looking for.
Many Thanks in advance.
#pragma strict
public var NameArray : String[] = ["Rugeley","Lichfield","Cannock","Stafford"];
var chosenName : String;
function ChooseName()
{
chosenName = NameArray[Random.Range(1,NameArray.length)];
}
function OnGUI()
{
GUI.Label (Rect (10, 30, 100, 100),"Edison Utd \n Vs \n " + chosenName);
}
In Unityscript, the default is public, so "public var" and "var" are the same. You should use "private var" to make chosenName a private variable. Also stick to using lowercase for variable names; it makes things easier to understand, so NameArray should be nameArray.
Answer by Eric5h5 · May 25, 2014 at 12:31 AM
You're not calling the ChooseName function anywhere. Also arrays start with 0, not 1.
Of Course!!! I added ChooseName(); Now it works exactly as it should!!!
$$anonymous$$any thanks!!