- Home /
Inspector serializing custom classes
Hi, I've created a namespace with some custom classes and a list in another class using those types.
The namespace:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InventoryDefs;
namespace InventoryDefs
{
[System.Serializable]
public abstract class Item
{
public int Price;
public int Rarity;
public float Mass;
public string FlavorText;
}
}
The list:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InventoryDefs;
public class MenuItemHandler : MonoBehaviour
{
public List<Item> PlayerInv = new List<Item>();
}
But it still doesn't show in the editor. Any ideas why?
Answer by Bunny83 · Oct 20, 2019 at 11:11 PM
Unity's serialization system does not support polymorphism for custom serializable classes. See script serialization for more details. Especially the sections "Serialization rules" and the following "When might the serializer behave unexpectedly?"
In essence custom serializable classes act like structs and are serialized inline based on the variable type. No type information is stored for them. You can examine your owh scene files or prefab files in a text editor (usually the asset serialization mode now defaults to "force text"). Here's a sample scene that Unity provides. The scene doesn't have any MonoBehaviour derived components. However for example the NavMeshSettings does have another sub class in "m_BuildSettings". Likewise "LightmapSettings" has the field "m_LightmapEditorSettings" which also contains a custom serializable class. As you can see, only data is stored, no type information.
Your answer
Follow this Question
Related Questions
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers
There is no GameObject attached to this GameObject 2 Answers
Script reference not showing in inspector 0 Answers
Add a new script line to my script with text from textboxes after button in the Inspector is pressed 0 Answers
Change Camera With a Script 1 Answer