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._