- Home /
C# Reflection - Get All Public Variables from Custom Script
Is it possible to get all of the public variables from a custom component script attached to a GameObject? If so, how is it done without any custom methods?
Why not search in the internet, i'm sure you could get the answer because its C#!
@Felix: If you know of an obvious and specific answer elsewhere, you're free to post a link to it as an answer to this question. Otherwise this seems to be a perfectly valid question for Unity Answers.
Answer by ricardo_arango · May 15, 2012 at 04:42 PM
You can use Reflection:
const BindingFlags flags = /*BindingFlags.NonPublic | */BindingFlags.Public |
BindingFlags.Instance | BindingFlags.Static;
FieldInfo[] fields = obj.GetType().GetFields(flags);
foreach (FieldInfo fieldInfo in fields)
{
Debug.Log("Obj: " + obj.name + ", Field: " + fieldInfo.Name);
}
PropertyInfo[] properties = obj.GetType().GetProperties(flags);
foreach (PropertyInfo propertyInfo in properties)
{
Debug.Log("Obj: " + obj.name + ", Property: " + propertyInfo.Name);
}
I think that should work fine on iOS - it's Reflection.Emit that doesn't. Have a look here: http://docs.xamarin.com/ios/about/limitations#System.Reflection.Emit
Or this previous Unity answer: http://answers.unity3d.com/questions/207670/using-reflection-with-ios.html
btw your BindingFlags are a bit agressive ;) The question was public variables ;)