0
不知道發生了什麼問題。將一些代碼外包到另一個類中存在問題。如果下面的代碼在同一個類中,它可以正常工作。Unity3D C# - NullReferenceException雖然附加了文本並且方法正常工作
SearchClient.cs
void callExposeAPi (string id)
{
ExposeClient exposeClient = (new GameObject("ExposeClient")).AddComponent<ExposeClient>();
exposeClient.loadExpose(id);
}
ExposeClient.cs
public Text _baserentText; // is attached to Text in Unity
public void loadExpose(string id)
{
[some API stuff...]
Debug.Log(result.exposeexpose.realEstate.baseRent); // 480
makeUseOfExposeUI(result.exposeexpose.realEstate);
}
void makeUseOfExposeUI (Realestate realestate)
{
Debug.Log(realestate.baseRent); // 480
_baserentText.text = realestate.baseRent.ToString();
}
錯誤仍然相同。在Unity Hierarchy中不再有ExposeClient作爲'GameObject'。 – fmielke
ExposeClient.cs已附加到'EventSystem'。我用'ExposeClient exposeClient = GameObject.Find(「EventSystem」)。GetComponent();'它的工作原理:) –
fmielke
@fmielke酷:) – Dilmer