通过Unity中的脚本访问公共2D数组变量

时间:2015-07-31 22:41:10

标签: arrays

所以我想出了如何通过下面的代码制作可在检查器中查看的2D数组。

[System.Serializable]
 public class QuestPhase {
     [HideInInspector]
     public string name;
     public int[] Phase = new int[5];
 }
 public QuestPhase[] questPhase    = new QuestPhase[5];

这很好用,但我正在试图弄清楚如何通过脚本访问这些信息,我无法理解。

 questVariable = questPhase[1,1];

返回“预期1索引,得2”的错误,只使用一个索引就可以“无法将类型QuestPhase转换为int类型”。我确定答案很明显,但如果有人能为我解答,我会非常感激。

1 个答案:

答案 0 :(得分:0)

当它显示expected 1 index, got 2时,因为您的questPhase变量是QuestPhase[] - 即QuestPhase s的一维数组。您只需按questPhase[1]索引它。

此时,您似乎正在尝试将questPhase[1]用作int,即使它实际上是Phase类型。您可能希望questPhase[1].Phase[1]获取第二个QuestPhase对象,并从中获取第二个Phase

questVariable = questPhase[1].Phase[1];