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 object of an object"오류가 발생한다는 것입니다.
내가 여기서 무엇을 놓치고 있습니까?
해당 개체에 대한 참조가 필요합니다.
클래스가 동일한 객체에 연결된 경우 StartGame
다음을 수행 할 수 있습니다.public genDicTable GEN = GetComponent<genDicTable>();
클래스가 다른 개체에 연결되어 있으면 public genDicTable GEN = GameObject.Find("YourOtherObjectName").GetComponent<genDicTable>();
수정 :
경우 genDicTable
스크립트가 존재하고 당신은 그것을 참조 할 :
public class StartGame : MonoBehaviour {
public genDicTable GEN;
private void Start()
{
GEN = GameObject.Find("your object's name").GetComponent<genDicTable>();`
Debug.Log(GEN.masterCount); // <-- This yields NULL.
}
}
경우 genDicTable
스크립트가 수행 되지 존재하고 당신은 단지 그것의 새로운 인스턴스를 만들려면 다음 확인 이 대답을.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다