【发布时间】:2021-10-30 18:07:29
【问题描述】:
我有一个 System.Serializable 类
[System.Serializable]
public class _answersData
{
public string word;
public string wordmeaning;
public string wordmeaning2;
public GameObject result;
}
然后我有一个 monobehaviour 类,其中声明了一个 _answerData 类的数组
public class gamePlay : MonoBehaviour
{
public _answersData[] answersData;
}
然后我尝试使用以下方法将数据从另一个脚本添加到 _answerData 数组
public class challengeScript: MonoBehaviour
{
void Start()
{
gameplayScript = gameObject.GetComponent<gamePlay>();
populate();
}
void populate()
{
answersCount = int.Parse(snapshot.Child("Answers").Child("Count").Value.ToString());
gameplayScript.answersData = new _answersData[answersCount];
for (int i = 0; i < answersCount; i++)
{
string positionNode = "Answer" + (i + 1).ToString();
string _word = snapshot.Child("Answers").Child(positionNode).Child("Word").Value.ToString();
gameplayScript.answersData[i].word = _word;
}
}
}
但我在 'gameplayScript.answersData[i].word = _word' 行收到 NullReferenceException
这是错误:
NullReferenceException:对象引用未设置为对象的实例
有人可以指导我在这里做错了什么吗?
【问题讨论】: