using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;
public class JSON : MonoBehaviour {
[MenuItem("Tools/CreateJSON")]
static void WriteJson()
{
string path = Application.dataPath + "/PartMessage.json";
GameObject obj = Selection.activeGameObject;
if (obj.name != "Part")
{
throw new System.Exception("Selection Model");
}
DateVO date = new DateVO();
for(int i = 1; i <= obj.transform.childCount; i++)
{
PartVO part = new PartVO
{
pos = obj.transform.GetChild(i).localPosition,
rot = obj.transform.GetChild(i).localEulerAngles,
scale = obj.transform.GetChild(i).localScale
};
date.parts.Add(part); //這行是不是有問題
}
string s = JsonUtility.ToJson(date);
File.WriteAllText(path,s);
AssetDatabase.Refresh();
}
uj5u.com熱心網友回復:
當前問題是空指標例外,這種例外在出現的時候,都會有詳細資訊顯示是第幾行出現。所以看到這個例外資訊的時候,完全可以精準定位到代碼所在的行
找到出錯代碼行后,判斷:物件.方法。或者是:物件.屬性等方式的。都是有可能引發這個例外的。
以20行舉例,這是個假設,出問題的是哪一行需要你控制臺獲取:
原本代碼是:
pos = obj.transform.GetChild(i).localPosition,
其中
obj.transform。是obj物件獲取transform屬性。如果obj是null,引發此例外
obj.transform.GetChild(i) 是obj.transform得到的transform物件獲取GetChild方法。如果transform是null,引發此例外。當然,當前情況下,不可能成立。因為如果成立,在obj.transform就觸發了
obj.transform.GetChild(i).localPosition 是obj.transform.GetChild(i)獲取到的transform物件獲取localPosition屬性,如果transform是null,引發此例外。
所以。一個空參考例外,引發的可能性較多。需要針對處理
uj5u.com熱心網友回復:
從代碼上來說,值得懷疑的物件是:date.parts.Add(part); //行是不是有問題
date.parts 這個物件應該是一個List,但是它沒有實體化物件,應該只是一個宣告,缺少實體化。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/45745.html
標籤:Unity3D
