引用c#中数组中对象的组件

本文关键字:组件 对象 数组 引用 | 更新日期: 2023-09-27 18:14:19

我试图做一个"东西",检查/取消检查一个特定对象的可交互复选框从一个数组通过使用玩家首选整数。我遇到的问题是我似乎无法从数组中引用特定对象,请帮助。

下面是一些脚本:

//This part is from the Start function.
for (int i = 0; i < buttons.Length; i++) {
            if (PlayerPrefs.GetInt("button" + i) == null) {
                PlayerPrefs.SetInt("button" + i, 1);
            }
            if (PlayerPrefs.GetInt("button" + i) == 1) {
                button.interactable = true;
            } else {
                button.interactable = false;
            }
        }

void Update () {
        for (int i = 0; i < buttons.Length; i++) {
            if (PlayerPrefs.GetInt("button" + i) == 0) {
                button.interactable = false;
            }
        }
    }

你可以看到button. interactitable = true/false的地方是我遇到问题的地方。

引用c#中数组中对象的组件

如果您没有在其他地方定义button;我假设你错过了数组索引访问器的概念;您可以使用buttons[i]而不是button