How do I get the index of an item in a for loop? (js)

I have a list of items that I want to display in their index order, displayed in the OnGUI function by a name variable in their class. I want to put each new item on the next line. How do return the index as an int so that I can say something like:

var indent : int;

var lineSpace : int;

function OnGUI(){

for(var i : Item in itemList){

GUI.Label( rect ( indent, lineSpace * (the index of i + 1), 20, 160,;



Ive been looking through the documentation for the generic lists but I cannot find out how do get the index number of of a variable. Thanks for the help in advance.

The foreach loop does not include an index, so there is no way to get one unless you keep track of it yourself (e.g. increment by one each time through the loop.

In your case, you would be better to use a regular for loop:

for (var i : int = 0; i < itemList.Count; i++)

For arrays that have unique value entries you can use IndexOf. Both in UnityScript and C#
Might not fit the question example, but good to know.

for(i in itemList){
 Debug.Log('Current index '+ itemList.IndexOf(i) );