Method for listing all variables inside Component script

Hey all,

I am trying to find out if there is a way to list all of the publicly exposed members of a Script attached to a GameObject as a Component.

i.e. in the Car Tutorial, We have a Car GameObject that has a Car (Script) which is attached to the GameObject as a component. Using GetComponentsInChildren() on the GameObject, I can find the Car (Script) Component. But is there a way to get a list of the script’s publically exposed variables without knowing their name beforehand?

Much appreciated!

-Markus

using System.Reflection

Type carType = CarComponent.GetType();
FieldInfo[ ] field = carType.GetFields();

for(int i = 0; i < field.Length; i++)
{
Debug.Log(field*.Name);*
}
Check out
FieldInfo Class (System.Reflection) | Microsoft Learn
for what you can do with FieldInfo.
(Field is a .NET term for Variable, similar to how Method is the term for Functions)

Thanks Ntero! I was using javascript but if the only way is to use C# then I guess that will have to do! If anyone knows if this is possible in javascript, please let me know. Thanks again.

-Markus

It should be possible in javascript.

You have access to the .NET library and all the methods.

I’m sorry, though I’m not as familiar with it.

Although it should just be a syntax change (use Import System.Reflection) as you should be able to access the same variable types. (emphasis on Should)