【发布时间】:2017-03-18 23:21:06
【问题描述】:
我有两个类,genDicTable 和 StartGame。我想在 StartGame 中从 genDicTable 中引用一个变量,但它产生 NULL。
genDicTable.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class genDicTable : MonoBehaviour
{
public TextAsset file;
public double masterCount;
private void Start()
{
Load(file);
masterCount = rowList.Count;
Debug.Log(masterCount); // <-- This properly prints out the value of masterCount
}
public class Row
{
public string id;
public string word;
public string length;
}
public List<Row> rowList = new List<Row>();
public void Load(TextAsset csv) {
// This function assigns a value into RowList
}
}
StartGame.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartGame : MonoBehaviour {
public genDicTable GEN;
private void Start()
{
Debug.Log(GEN.masterCount); // <-- This yields NULL.
}
}
所以,问题是当我访问 StartGame.cs 中的变量 masterCount 时,它会产生错误“NullReferenceException: Object reference not set to an instance of an object。”
我在这里错过了什么?
【问题讨论】:
-
正如它所说的值为空。这种行为没有错。您尚未在 startgame.cs 中为 genDictTable 类型的变量 GEN 创建任何实例
-
请仔细阅读指出的副本。如果你不知道什么是实例以及如何初始化它,那么这里给出的答案都不能帮助你理解你的问题并避免将来因为同样的原因而出现失误