Create editor window with ScriptableObject.CreateInstance

I'm positing this as an answer, not a question, because I couldn't find the answer anywhere else.

Problem: If you have a custom editor window (class extends EditorWindow) and try to invoke it like this:

var window : MyCustomWindow = new MyCustomWindow();

You'll get the following warning upon invocation:

MyCustomWindow must be instantiated using the ScriptableObject.CreateInstance method instead of new MyCustomWindow.

However, there is no documentation to appropriately instruct how to do this. My first attempt was this:

var window : MyCustomWindow = ScriptableObject.CreateInstance("MyCustomWindow");

Which produces the error:

Instance of MyCustomWindow couldn't be created because there is no script with that name.


var window : MyCustomWindow = ScriptableObject.CreateInstance(typeof(MyCustomWindow));

Hopefully this saves someone else a little time.

Here is a solution that works for dynamically creating the object given a string. This is for C#.

MyBase v_new_instance = null;
Type v_type = Type.GetType(v_class_name);
if (null != v_type) {
    v_new_instance = (MyBase)CreateInstance(v_type);

Yes, well, what if all I have is a string containing the type? I need to dynamically create an object based on a string selected from a list of script names read from a directory. This means I can’t statically specify the type as shown in this solution. I need to be able to call CreateInstance with a string - from C#.

now this is the way

GUISkin a = ScriptableObject.CreateInstance();