- Home /
Problem making buttons from array of array
I'm having some trouble with creating buttons from an array again. This time I'm trying to access the first element of every item in the inventory list. I keep getting multiple errors every way I try it. After eliminating parts to determine the issue it appeared to be [item] in this code below in most cases.
for (var item in ItemList) {
if (GUILayout.Button(ItemList[item][0], GUILayout.Height(wh2 * 0.11))) {
}
}
I also tried this.
for (var item in ItemList) {
if (GUILayout.Button(ItemList[item[0]], GUILayout.Height(wh2 * 0.11))) {
}
}
I did try declaring item as an array but it didn't work. I also tried using the other way of creating buttons from an array but that didn't work either. I tried both of these using the other way. In case you don't know what I'm talking about this is what it looks like.
for (var item : int = 0; item < ItemList.Length; item++) {
if (GUILayout.Button(ItemList[item][0], GUILayout.Height(wh2 * 0.11))) {
}
}
Finally I also tried declaring the array two different ways.
static var ItemList : Array[];
static var ItemList = new Array();
Each way seems to be giving me at least one unique error. I believe I'm going in the right direction by declaring the array the second way.
What are the things in ItemList? You should really be using generic collections see this Unity Gems article: http://unitygems.com/gotchas
ItemList contains the arrays for every item in the inventory. Why should I bother using generic collections?
Sorry, I meant what is an "item in the inventory"? You should bother because it tells the compiler (and me) what's going on. It gives specific error messages when you do something wrong and its faster.
A series of strings, floats, and integers. I guess I'll take a look at them if it'll increase the speed.
Answer by DoctorTodd · Jan 30, 2013 at 05:41 AM
I gave it another shot and this appeared to have fixed it.
static var ItemList = new Array();
for (var items : Array in Menu.ItemList) {
if (GUILayout.Button(items[0], GUILayout.Height(wh2 * 0.11))) {
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Label an array of buttons with an array of strings 2 Answers
[Solved]Using an array of two buttons? 1 Answer
Array out of range! 1 Answer
Change color of all buttons listed in array 3 Answers
Creating Buttons from a Button created from an Array 1 Answer