lundi 18 janvier 2016

How do I turn the result of GetField() into a usable object?

In the code below, the class ButtonScript has a field called buttonObj which is of type GameObject

var button = gameObject.AddComponent<ButtonScript>();
var obj = button.GetType().GetField("buttonObj");
Debug.Log(obj); //prints UnityEngine.GameObject
Debug.Log(obj.name);  //compilation error

The error on that last line is:

Type 'System.Reflection.FieldInfo' does not contain a definition for 'name'... 

Why does it say it's a GameObject when it's logged, but says it's a FieldInfo object when I try to use it?

How can I get it so that I can treat it like aGameObject?





Aucun commentaire:

Enregistrer un commentaire