x


Array doesn't appear in GUI

Hi I just learn about Arrays... After run under UNITY and clicking on the Button Array doesn't appear. Can anyone tell me what is wrong with code below?

var myCars3=["Saab","Volvo","BMW"];

function Start () {
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
}

function OnGUI(){
if(GUI.Button(new Rect(10,10,100,30), "Show Array"))
 ShowArray();
}

function ShowArray(){
 for(i=0;i<myCars3.length;i++)
 GUI.Label(new Rect(10,10 +i*30, 120,30), "This is" +myCars3[i]);
}
more ▼

asked Jul 03 '12 at 10:21 PM

erghard gravatar image

erghard
16 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The function ShowArray is only called the frame when the button was pressed. Also, your Start function has no effect. Try this instead:

var myCars = ["Saab","Volvo","BMW"];
var showingArray = true;

function OnGUI(){
  if(GUI.Button(new Rect(10,10,100,30), "Toggle Array"))
    showingArray = !showingArray;

  if( showingArray )
    ShowArray();
}

function ShowArray(){
 for(i=0; i < myCars.length; i++)
   GUI.Label(new Rect(10,10 +i*30, 120,30), "This is" + myCars[i]);
}
more ▼

answered Jul 03 '12 at 10:33 PM

Mortoc gravatar image

Mortoc
915 2 7 14

Should be myCars.Length.

Jul 03 '12 at 11:26 PM Eric5h5

thanks Mortoc ;)

@Eric5h5 - is there any semantic diffirence between length and Length? it works anyway.

Jul 04 '12 at 10:11 PM erghard

That's weird, in Mono/.NET it has to be "Length". The Javascript Array class uses "length". I guess they made "length" work with .NET arrays in Unityscript.

Jul 04 '12 at 11:08 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3689
x1363

asked: Jul 03 '12 at 10:21 PM

Seen: 251 times

Last Updated: Jul 04 '12 at 11:08 PM