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:
I’ve referenced the following pages and videos (next to googling everywhere):
-
Unity Pro Tip: Use custom made
assets as configuration files -
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.