Converting a string into a type, then using that type in a script.

So to begin with, I’m writing an editor script that serves the following purpose: It will be able to access any MonoBehaviour (user defined) and access any list on that MonoBehaviour (again user defined), and will then display this list in a user friendly manner.

The script looks a little bit like this:

public class ListOrganizer : EditorWindow
        System.Type targetType;//This is the type that we are using in our list
        public System.Collections.Generic.List<Resource> list =
            new System.Collections.Generic.List<Resource>();
        public Resource classType = new Resource();
        FieldInfo[] fieldInfos;
        public ShipController sourceScript;//This is the script with the list we will be editing
        public string listName = "Target List Name";//This is the name of the list variable that we want to edit
        bool editing = false;
        public static void Init()
            ListOrganizer listOrganizer = (ListOrganizer)EditorWindow.GetWindow(typeof(ListOrganizer));
        void OnGUI()
                sourceScript = EditorGUILayout.ObjectField(sourceScript, typeof(ShipController), true) as ShipController;
                listName = EditorGUILayout.TextField(listName);
                if(GUILayout.Button("Start Editing"))
                    if(sourceScript != null)
                        list = (System.Collections.Generic.List<Resource>)sourceScript.GetType().GetField(listName).GetValue(sourceScript);                     
                        editing = true;

        ...//Display all the editing information

Apart from one major error, it’s working really well. However, currently this code only works for displaying List*Resource* types and only from the MonoBehaviour ShipController. What changes do I have to make so that I can grab List*ANYTYPE* from MonoBehaviour [ANYSCRIPT]?

Everything I’ve been able to find points me to something like what is described here. How do i convert a String into a Class reference in C# - Unity Answers

So what I would want is something like System.Type targetType = typeAsAString.GetType().

But then how do I get the type based an a string, then utilize that type throughout the rest of this script?

NOTE: Not sure why but I can’t use “<” and “>”. Edited to clear up the confusion.

I’m not sure what you are after. Are you looking for this?

System.Type myType = Type.GetType(SomeClass);

I believe you’re looking for Activator.CreateInstance:

System.Type UserDefinedType = Type.GetType("UserDefinedClassString");
object myTypeInstance = Activator.CreateInstance(UserDefinedType, {constructor params});

I think you try to create a “general purpose editor” which actually doesn’t make much sense. In Unity we only have compiled languages with strong typing of variables. According to OOP and inheritance you can generalize a reference, but then you loose the access to any specific properties. For example, you could change your ObjectField to allow any of your script instances to be selected by replacing typeof(ShipController) with typeof(MonoBehaviour). Now the returned value can’t be casted to ShipController since it’s now just a MonoBehaviour and all things specific to a certain class like your ShipController class can’t be accessed. Only things available in the base class which is MonoBehaviour in my example.

You can however use reflection to access dynamically any type of class / class member, but you have to use reflection for everything in this case. Something similar to this has already been done in Unity: The serialization system. This is a huge framework and only supports some basic types.

I’ve once made a very (very) simple inspector window which works at runtime in my Android build. I could select and object in the scene, browse through it’s components, see it’s fields / properties / methods and could view / edit a few types (int, string, float) and could call parameterless methods. This was already a quite huge framework and a lot of work.

If you really want to implement something like that, you should get familiar with the whole reflection namespace and how the System.Type object works.

Like others said it’s hard to understand what exactly you want to do. What does “access” mean to you? What’s the purpose of that window anyways? The InspectorWindow does most of those things already. Maybe you just want to create some CustomEditors or PropertyDrawers to modify the view of the Inspector? Creating an inspector from scratch would be crazy.

IMPORTANT: write “using System;” to the top of the script, this is necessary because it NOT WORKS with “System.Type” for some reason!

Type stringToType(string from)
return Type.GetType(from);