私はgenDicTableとStartGameという2つのクラスを持っています。私は、StartGameのgenDicTableから変数を参照したいが、NULLを返す。別のクラスの変数を参照すると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得られることである:オブジェクト参照をオブジェクトのインスタンスに設定されていません。
私はここで何が欠けていますか?
値がnullであると言います。この動作に問題はありません。あなたは、startgame.csのgenDictTable型の変数GENのインスタンスを作成していません。 –
尖った複製を注意深く読んでください。インスタンスとそれを初期化する方法がわからない場合、ここで与えられた答えはあなたの問題を理解し、同じ理由で将来の間違いを避けるのに役立ちません – Steve