Public Class variable not showing in inspector

Hey there. Pretty new dev here, relatively rusty.
I have a public abstract class named ‘Item’. The following code is a simplified version of it:

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

[System.Serializable]//Base Item class
public abstract class Item
{
    protected string ItemName;//Has the item's name
    protected string ItemTitle;//Has the item's type
    protected int ItemNum;//Has the item's special number
    protected string ItemDesc;//Has the item's description
    protected float Energy;//Has the item's energy level
    protected int HP;//Has the item's current HP
    protected int MaxHP;//Has the item's max HP
    protected int ShieldHP;//Has the item's shield HP
    protected Augment Augment;//Has the item's possible augment


    public Item (string ItemName,string ItemTitle,int ItemNum,string ItemDesc, float Energy, int MaxHP)//Creation without augment
    {
        this.ItemName = ItemName;
        this.ItemTitle = ItemTitle;
        this.ItemNum = ItemNum;
        this.ItemDesc = ItemDesc;
        this.Energy = Energy;
        this.HP = MaxHP;
        this.MaxHP = MaxHP;
        this.ShieldHP = 0;
    }
       
        //Following are basic get/set functions and what not

}

I have GameObjects with scripts attached to them, which are relatively empty except for the public Item variable:

using UnityEngine;
using System.Collections;

public class Stats : MonoBehaviour
{
    public Item MyItem;
}

However, when the script is attached to GameObjects in my scene, the inspector doesn’t show the public MyItem variable. What gives? It’s not a compiling error, because when I create simpler public variables (Like public int MyInt) they do show up.
I am feeling like I am missing something pretty basic here. Please help!
Thanks in advance.

First of all, Item is an abstract class, so it can’t be instantiated. That’s a problem.

Secondly, Item doesn’t have any public (or [SerializeField] data, so there’s nothing to draw.

Third, Unity doesn’t handle serializing inherited field out of the box. So you can put Item subclasses in the field, if that was what you were planning.

2 Likes

The unity serializer can only handle concrete types. No interfaces, no abstract classes.

Furthermore unity serializer serializes to the type the field is defined as, not the type of the object stored that. So if you had a ‘ItemA : Item’ and stuck it in that field… it would get serialized as an ‘Item’, not as a ‘ItemA’.

1 Like

Right, Thank you guys for your quick responses. I need this class abstract for now, so I’ll think of some other way to do what needs to be done.

I like to put the data in a separate file and load it at runtime. Basically run your own serializer.