Detecting the variables for a script or comonent - csharp

I need to be able to detect what variables associated with another script or component in c sharp. For instance, for renderer, i need all of the renderer.'s, so like renderer.matirel etc. I need both the ability to access them and the string name for them.

Please go through the Beginner Scripting module in Learn Unity. If after watching all the videos, (hint, pay attention at video # 20), you still have questions, post a comment here or ask a new question.

EDIT: I thought you knew all the variables in the object you had access to. Because you don’t, you can use Reflection to get all kinds of information, incl. field names and methods, out of your target. Unless I understood you wrong again…

You might not need use reflection, if your target is a unity class and you know the type, you can use GameObject.GetComponents()… which returns an array of Components. you can then loop over that array. Hope I understood you correctly this time.

Using C# reflection API. The code is not tested.

using System.Reflection;

try
	{
		// Get the FieldInfo of renderer
		FieldInfo[] myFields = renderer.GetFields(BindingFlags.Instance | 
				   BindingFlags.Static |
				   BindingFlags.NonPublic |
				   BindingFlags.Public);
		// Show values and names
		for(int i = 0; i < myFields.Length; i++)
		{
			Debug.Log ("The value of " + myFields_.Name + " is: " + myFields*.GetValue(myInstance));*_

* }*
* }*
catch(Exception e)
* {*
* Debug.Log(“Exception :” + e.Message);*
* }*