我在Unity3D上开发,我尝试用实现这里的类制作一个组合框
现在在我的考试课上,我这样做:
public class combobox_test : MonoBehaviour {
public GUIContent[] comboBoxList;
private ComboBox_Class comboBoxControl = new ComboBox_Class();
public GUISkin mySkin;
void start(){
comboBoxList = new GUIContent[5];
comboBoxList[0] = new GUIContent("Thing 1");
comboBoxList[1] = new GUIContent("Thing 2");
comboBoxList[2] = new GUIContent("Thing 3");
comboBoxList[3] = new GUIContent("Thing 4");
comboBoxList[4] = new GUIContent("Thing 5");
}
void OnGUI(){
GUI.skin = mySkin;
int selectedItemIndex = comboBoxControl.GetSelectedItemIndex();
selectedItemIndex = comboBoxControl.List(new Rect(50, 100, 100, 20), comboBoxList[selectedItemIndex].text, comboBoxList,GUI.skin.GetStyle(""));
//GUI.Label( new Rect(50, 70, 400, 21),"You picked " + comboBoxList[selectedItemIndex].text + "!" );
}
}我有个错误:
IndexOutOfRangeException:数组索引超出了范围。combobox_test.OnGUI () (位于资产/combobox_test.cs:56)
我尝试了一些我在不同网站上找到的解决方案,但都没有效果。
发布于 2012-05-15 13:13:21
当试图访问数组中大于或等于其长度的索引时,会引发IndexOutOfRangeException。我相信你的问题是
int selectedItemIndex = comboBoxControl.GetSelectedItemIndex();给出的值大于comboBoxList的容量(例如>= 5),因此当您试图访问comboBoxList[selectedItemIndex]时,会抛出IndexOutOfRangeException。
https://stackoverflow.com/questions/10601387
复制相似问题