Only one inherited class gets Serialized

I have one main class called GeneralGameStats

using System;

[Serializable]
public class GeneralGameStats : DataToSave
{
    public int hitsTaken;
    public float mineralsGathered;
    public float timePlayed;
    public int tricksUsed;
    public int deaths;
    public int glideTime;

    public virtual void Reset() { hitsTaken = 0; mineralsGathered = 0; timePlayed = 0; tricksUsed = 0; deaths = 0; glideTime = 0; }

}

Then 3 classes will be extending this class

public class LifetimeStats : GeneralGameStats
{

}

public class GameSessionStats : GeneralGameStats
{

}

public class DailyStats : GeneralGameStats
{
    public int gamesPlayed;

    public override void Reset()
    {
        base.Reset();
        gamesPlayed = 0;
    }
}

I then add all these classes on a Scriptable Object called PlayerCharacteristics

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

[CreateAssetMenu(fileName = "PlayerStatistics", menuName = "Player Statistics", order = 1)]

public class PlayerStatistics : ScriptableObject
{
    [SerializeField]
    private DailyStats DailyStats;
    [SerializeField]
    private LifetimeStats LifetimeStats;
    [SerializeField]
    private GameSessionStats LastGameSessionStats;

All Classes have the SerializeField Header and GeneralGameStats has the Serializable Header

When the Scriptable Object is created in Unity, only LastGameSessionStats field is serialized.

Am I missing something here? Shouldn’t all fields be serialized with no issue?

Capture

The Serializable attribute isn’t inherited, you need to put it on every subclass you want to serialize.

The odd thing is that LastGameSessionStats is showing up. When I tried your code, no fields were shown on the scriptable object. Maybe the posted code doesn’t exactly reflect the code from when you were testing or there’s multiple GameSessionStats?

1 Like

This is the solution, Although I already tried to add Serializable to all classes the first time it wasnt reflected on the editor. Must have been a reflection issue on the editor since the second time around it worked fine