C# - Requesting assistance with Reflection

I’m currently creating a level editor for my 3D platformer game. I’ve been making a lot of progress, but it looks like I’m going to require System.Reflection for this part. Basically I want to mimic Unity’s inspector but with reduced functionality so users can only edit certain things on the object (in this case only on the scripts). I’ve managed to get the following functionality working so it displays the type of each field, but I can’t get it to display any sort of value that I can work with. Here’s my current loop for each field inside each script.

MonoBehaviour[] scripts;
scripts = selected.GetComponentsInChildren<MonoBehaviour>();
for(int i = 0; i < scripts.Length; i++)
{
	BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
	FieldInfo[] fields = scripts*.GetType().GetFields(flags);*
  • for(int j = 0; j < fields.Length; j++)*
  • {*
    GUILayout.TextField(fields[j].FieldType.ToString());
  • }*
    }
    Would I even be able to edit values in this way, or would I only be able to view the information? As you can probably tell I’ve never used reflection before so it’s all a tad confusing. I tried doing the following:
    scripts_.GetType().GetProperty(fields[j].Name).GetValue(scripts*, null);*
    But that throws a null reference exception. Any help or beginner resources for reflection that I can read through would be appreciated._

With the GetValue method you can only get the value. If this value is reference type you can modify it’s properties (The name of a person for example), but you will not be able to assign a new Person to that value. You can do that with the SetValue Method (You can check it in [here][1]). Btw, what exactly gives the NullReferenceException? The property? I think that is because you are trying to get a property with a field name, and no such property exists. The fields are the private members and the properties are the public ones.

Try this:

scripts_.GetType().GetField(fields[j].Name).GetValue(scripts*);*_

_*[1]: Microsoft Learn: Build skills that open doors in your career