ScriptableObject.CreateInstance returning null value.

I’m guessing it’s because of “AddAssetSetWindow”, but I don’t know why that would be.

public abstract class AssetManagerWindow<T> : EditorWindow where T : IAsset
{
    public AssetManagerWindow()
    {
        var a = ScriptableObject.CreateInstance<AddAssetSetWindow<T>>();
        //a is null...
    }
}

public class AddAssetSetWindow<T> : EditorWindow where T : IAsset
{
}

What kind of window framework are you trying to build? Are you wanting to have an AssetManagerWindow for every type of asset? Or just an AddAssetSetWindow for every type of asset?

Did you post that question? If so, you should try a lower case manager variable within Test.

1 Answer

1

The problems i see here are:

  • classes derived from ScriptableObject need to be defined in a seperate file where the filename matches the classname (just like MonoBehaviours).
  • Such classes can’t be generic, never. You can have a generic base class, but the actual class(es) that should be created have to be non generic. Otherwise CreateInstance wouldn’t work with a string that contains the classname since a generic type can’t be described with a type-string.
  • EditorWindows are usually created with GetWindow. If you want you can use CreateInstance but don’t forget to call a Show method..

Instance of OptionsWindow`1 couldn't be created because there is no script with that name. UnityEngine.ScriptableObject:CreateInstance() AssetManagerWindow:CreateChildWindow() (at Assets/medellin/Editor/AssetManagerWindow.cs:39) AssetManagerWindow:OnGUI() (at Assets/medellin/Editor/AssetManagerWindow.cs:31) UnityEditor.DockArea:OnGUI() That proves your 2nd point, lol. Passing a type into the templated method throws that additional error. Since the name of the script has to match the class definition for it to work, it would be impossible to use a generic ScriptableObject in this manner.

Never even knew that existed. Learned something new :p thanks

@medellin: The article you linked is about Activator.CreateInstance which is a class that belong to the Reflection namespace and has nothing to do with Unity. With that class you can create "normal" classes via a System.Type reference instead of using the "new" operator and the concrete classname. ScriptableObject.CreateInstance is a function that comes with Unity and is the only way to create a ScriptableObject (properly). It has no relation to Activator.CreateInstance.