0
在下面的代碼,該類ButtonScript
有一個名爲buttonObj
字段,類型爲GameObject
如何將GetField()的結果轉換爲可用對象?
var button = gameObject.AddComponent<ButtonScript>();
var obj = button.GetType().GetField("buttonObj");
Debug.Log(obj); //prints UnityEngine.GameObject
Debug.Log(obj.name); //compilation error
上最後一行的錯誤是:
Type 'System.Reflection.FieldInfo' does not contain a definition for 'name'...
它爲什麼說這是一個GameObject
時它已記錄,但是當我嘗試使用它時,它說這是一個FieldInfo
對象?
我怎樣才能得到它,以便我可以像GameObject
那樣對待它?
的'的'ToString'可能FieldInfo'返回的字符串。 –
這是一個簡單的錯字?屬性名稱是'Name',而不是'name'。 –
@YacoubMassad否,小寫'name'是對的http://docs.unity3d.com/ScriptReference/Object-name.html – Houseman