【问题标题】:Referencing components of objects from an array in C#在 C# 中从数组中引用对象的组件
【发布时间】:2017-03-05 01:53:18
【问题描述】:

我正在尝试制作一个“东西”,通过使用播放器首选项整数从数组中检查/取消选中特定对象的可交互复选框。我遇到的问题是我似乎无法从数组中引用特定对象,请帮忙。

这里有一些脚本:

//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.interactable = true/false 的区域是我遇到问题的地方。

【问题讨论】:

  • 您是否考虑过在类中只使用一个按钮实例并将其显示在表单上,​​而不是在类中使用整数变量?这样你就可以通过类的实例与按钮进行交互了。

标签: c# arrays boolean components


【解决方案1】:

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

【讨论】:

  • 天哪,它成功了,非常感谢:D 对不起,我对 C# 太陌生,以至于我仍然习惯于不同东西所需的语法。
猜你喜欢
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
相关资源
最近更新 更多