Scriptable Object loose object type reference

Hi !!
Today I faced a quite strange problem related with ScriptableObjects, I’m doing an inventory tool that handles the objects created, assigning different types to them etc…

Here I have the basic data of the Objects :

namespace Inventory
{
    [System.Serializable]
    public class ObjectInventory
    {
        [SerializeField]
        public TYPE_OBJECT_INVENTORY type_object;
        [SerializeField]
        public string name_object;
        [SerializeField]
        public string description;
        [SerializeField]
        public Sprite object_icon;
        [SerializeField]
        public bool is_stackable;
        [SerializeField]
        public int max_capacity;
        [SerializeField]
        public GameObject go_picked;
        [SerializeField]
        public string name_script_obj;     
    }
}

Then I have my Asset who derives from ScriptableObject:

[System.Serializable]
public class ObjectAsset : ScriptableObject
{
   
    [SerializeField]
    public ObjectInventory obj;
}

And Finally one derived class from ObjectInventory that only keeps one GameObject.

namespace Inventory
{
    [System.Serializable]
    public class ToolObjectInventory : ObjectInventory
    {
        [SerializeField]
        public GameObject tool_object;
    }
}

Everything works fine, in the editor, if I know that I’m creating a Type of object that is a tool I jus simply do the following:

 new_asset = ScriptableObject.CreateInstance<ObjectAsset>();
            new_asset.obj = new ToolObjectInventory();

And I can modify the class values and everithing, the problem comes when I hit the Play Button, the type of the object saved in the ObjectAsset just changes to ObjectInventory, the Base class, loosing all the information saved previously.

I used AssetDataBase.Save and SetDirty() to save all the data and the rest of the information like names,icons,descriptions etc is saved correctly.

Please if somebody would give some hint about what’s happening I would be really grateful.

THANKS.!

Try using AssetDatabase.AddObjectToAsset. There’s an example here.

Hi TonyLi thanks for your answer.

The problem with your solution is that you are using scriptable objects to store data while I am using simple classes to handle it, and the asset itself is the ScriptableObject.

I will give a try to your solution !

This is a problem with the Unity serialization system - it doesn’t handle inheritance for classes that are not scriptable objects or MonoBehaviours.

When it serializes the ObjectInventory field on your ObjectAsset, it serializes it with the type ObjectInventory, no matter what subclass you actually used there. A simpler example: If you have a field of the type Animal, and put a Cat there, it’s going to serialize an Animal (even if Animal is abstract!).

I’ve used some workarounds:

  • have the ObjectInventory and ToolObjectInventory be SerializeableObjects too. That’ll make everything work out of the box, but will clutter your project with a bunch of hard-to-manage SerializeableObjects.
  • Don’t use inheritance here, and find a way to make your script work without it. Uglier code, easier serialization.
  • Using ISerializationCallbackReceivers to serialize data about what subclass the object was, and what data was stored, and then write that information back on deserialization. This has the largest programming workload, but gives the result you want.
1 Like

Good workarounds, all helpful to mention.

You don’t have to save the XXXInventory objects as individual asset files. You can use AssetDatabase.AddObjectToAsset to add them to the parent asset. This way there will still only be one file in the project, which reduces clutter.

Thanks a lot guys for your time!!

I didn’t know that unity didn’t serialize the inherited classes, my fault there.

Right now I’m going for this option (Simplest one):

Some messy code there, but i can’t spend much more time on it.

Completely true TonyLi, I did it that way on the past, but I choose to create 1-asset/1Object for more easy workflow along with my artist. (Folders structure, and merging assets will be easier too I think)

Again thanks both of you guys!!