0

我在 Unity 3D 上开发,并尝试使用此处实现的类制作一个 Combobox

现在在我的测试课上,我这样做:

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 () (在 Assets/combobox_test.cs:56)

我尝试了一些在不同网站上找到的解决方案,但没有任何效果。

4

1 回答 1

1

尝试访问数组中大于或等于其长度的索引时,会引发 IndexOutOfRangeException。我相信你的问题是

 int selectedItemIndex = comboBoxControl.GetSelectedItemIndex();

给出的值大于comboBoxList(例如>= 5)的容量,因此当您尝试访问comboBoxList[selectedItemIndex]IndexOutOfRangeException 时会抛出。

于 2012-05-15T13:13:21.410 回答