- Home /
Is there a way to get a list of properties (variables) on Scripts on an object?
I know I can find a list of scripts (components) on an object thus:
Component[] ss = (Component[])oldTr.GetComponents (typeof(Component));
The question is, is there a way to get the 'custom attributes' or properties or variables on those scripts, without knowing the script's API, so to speak?
In other words, I want to walk through all scripts attached to an object and get all the variables (name and value) for each.
Actually the question becomes "Is there a way to get arbitrary variables off a script, if you don't know the script beforehand?" because this will get all the scripts: Component[] cs = (Component[])go.GetComponents (typeof(Component));
Answer by DaveA · Sep 29, 2010 at 01:46 AM
Too slow!
Here's some nuggets:
Component[] cs = (Component[])oldTr.GetComponents (typeof(Component));
foreach (Component c in cs)
{
Debug.Log("name "+c.name+" type "+c.GetType() +" basetype "+c.GetType().BaseType);
foreach( FieldInfo fi in c.GetType().GetFields() )
{
System.Object obj = (System.Object)c;
Debug.Log("fi name "+fi.Name+" val "+fi.GetValue(obj));
}
Answer by dingben · Oct 14, 2010 at 08:13 AM
DaveA... beautiful stuff
This is a comment - comments just don't allow space and formatting.
I was looking for the same feature but could not find any docs on those methods. Can you point to the documentation that covers them?
I am not a C# programmer, I use UnityScript, but I trimmed down your code to get it passed the assembly reference error: (...I am not sure what should be in the 'using (Ref);')
Component[] cs = g.GetComponents<Component>();
foreach (Component c in cs)
{
Debug.Log("@@ " +g.name +"\t["+c.name+"] "+"\t" +c.GetType() +"\t"+c.GetType().BaseType);
}
'g' is GameObject var in outer loop. The @@@ makes it easy to find the lines in the log.
The output is exactly what I needed: component name&type list.
Beautiful! Thank you.
This reference (and pages it links to) were the most helpful: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
WILL look at doc soon... I just updated my Debug.Log line above to include the names of the children GameObjects. So this is complete now... i think. I am learning C# syntax by trial and error messing with this stuff. LOL
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Component Array 0 Answers
Use enum value as a index variable of a list 1 Answer
Adding an attribute/type to a transform or object 0 Answers
Get GameObject from List based on attached script or assigned name 3 Answers