Custom Editor ScriptableObject List shows "Type Mismatch" after CreateInstance

I’ve been trying to get a ScriptableObject to contain a list of ScriptableObjects. I created an Asset for my container class called MyScriptableObjectList that should hold the MyScriptableObject elements.


What I’m doing:

With a CustomEditor I’ve added a button that adds new ScriptableObject instances to the list by doing:

items.Add(CreateInstance<MyScriptableObject>())

Problem:

But apparently this gives me elements showing up as:

Type mismatch


These are my simplified test scripts that show the problem:

SerializeMeList.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

public class SerializeMeList: ScriptableObject 
{
    public List<SerializeMeList> items;

    [MenuItem("Test/SerializeMeList")]
    public static void CreateAsset()
    {
        ScriptableObjectUtility.CreateAsset<SerializeMeList>();
    }
}

SerializeMe.cs

using UnityEngine;
using System.Collections;
using UnityEditor;

public class SerializeMe: ScriptableObject
{

    public float power;

    [MenuItem("Test/SerializeMe")]
    public static void CreateAsset()
    {
        ScriptableObjectUtility.CreateAsset<SerializeMe>();
    }

}

Editor/SerializeMeListEditor.cs

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(SerializeMeList))]
public class SerializeMeListEditor : Editor {


    public override void OnInspectorGUI()
    {
        SerializeMeListmyList = target as SerializeMeList;

        this.DrawDefaultInspector();

        if (GUILayout.Button("Add Serialize Me Item"))
        {
            myList.items.Add(CreateInstance<SerializeMe>());
        }
    }
}

And I’m using this ScriptableObjectUtility:

CreateScriptableObjectAsset

I’ve referenced the following pages and videos (next to googling everywhere):

  1. Unity Pro Tip: Use custom made
    assets as configuration files

  2. Unity Blog: Unity Serialization

  3. Serialization in-depth with Tim
    Cooper: a Unite Nordic 2013
    (watched this about 3 times, very nice!)

Additional Information

I’m trying to make an Item Database. I would preferrably design this as an OO (with inheritance) hierarchy of Items, like Weapon inherits from Item, SpecialWeapon inherits from Weapon.

I want to make a single database that contains ids for all the different Items. Preferrably I would have the items in the list also show the variables of inherited types of Item, possibly through reflection (or some other automated way). Tips related to the problem or workarounds to make a better Inventory system (with or without scriptableObjects) are very welcome, preferrably in the comments as it would be somewhat unrelated to the title of this question.

EDIT:
So I’ve found this Unity Answers Question & Answer to provide me with some new information: [CustomInspector] add scriptableobject in a list always null

According to the answer what I’m trying to do is impossible and every single instance I try to create and add to the list should be an .asset file by itself. Since I’m not sure whether this is the case I’ll keep this question/answer open to see if any other solutions come by or someone confirms this.

Actually you can do it.

I was trying to do the same and indeed, you have to create a single asset for each item (class), but not necessary a .asset since you could use the AssetDatabase.AddObjectToAsset method (and use HideFlags.HideInHierarchy tag for the asset to be whole).

Since the question got already bumped, here’s the actual answer to the question:

The generic List inside the “SerializeMeList” class doesn’t use the type “SerializeMe” but “SerializeMeList”. A “SerializeMeList” is not a “SerializeMe” instance. I guess this is actually a typo and you wanted a:

public List<SerializeMe> items;

instead of

public List<SerializeMeList> items;

The “answer” @eclectyc wrote might be helpful as well, but doesn’t answer the question.