Earlier, I tried making characters using Scriptable Objects. But, the values would always change to null at runtime. I imagine I was doing something wrong. However, I have since decided to just make scripts for each character that derive from an abstract Character class.
Since I want to be able to easily add a sprite for each character’s profile, how do I get the single field to show in the editor? I’ve tried Serializing the entire class, the field itself, and making it public, but nothing seems to work. Since the class derives from an abstract class and not a mono-behavior, this is much more difficult than I would have imagined.
Character.cs
public abstract class Character
{
public enum Job
{
Tank,
Healer,
Mercenary
}
public abstract string Name { get; set; }
public Job job;
public Sprite profilePicture;
public abstract int Health { get; set; }
public abstract int Attack { get; set; }
public abstract int Magic { get; set; }
public abstract int Defense { get; set; }
public abstract int Heal { get; set; }
public abstract int CritRate { get; set; }
public abstract int Resistance { get; set; }
}
Stella.cs
[System.Serializable] public class Stella : Character
{
[SerializeField] public new Sprite profilePicture;
public Stella()
{
Name = "Stella";
Health = 50;
Attack = 0;
Magic = 5;
Defense = 0;
Heal = 0;
CritRate = 5;
Resistance = 0;
job = Job.Mercenary;
}
public override string Name { get; set; }
public override int Health { get; set; }
public override int Attack { get; set; }
public override int Magic { get; set; }
public override int Defense { get; set; }
public override int Heal { get; set; }
public override int CritRate { get; set; }
public override int Resistance { get; set; }
}
Properties are not shown in the Editor. Use fields. If you need, you can leave properties for access from other classes and use private fields to show in the Editor. Use [SerializeField] to show private fields in the Editor.
Edit : In order to add code formatting to text, you need to place 4 spaces at the begining of the line. If you are doing it manually.
Note that you said you originally derived your class from ScriptableObject. Is your abstract “Character” class derived from anything? Because Unity’s serialization system does not support polymorphism for custom serializable classes. Only for ScriptableObjects and MonoBehaviours.
Using the “new” keyword in field declaration is in most cases not a good idea. That means you already have a field with that name and you introduce a new seperate field with the same name. So in the different contexts (parent or child class) the same name refers to different fields. The new keyword just hides derived members, though they are still there but can only be accessed through base.fieldname. Since a class derived from a base class actually is also a base class you don’t need to declare the field again.
First i would recommend to read this page carefully. Second it you still need help with this you should include your base class(es) so we can tell you if your class is serializable or not.
Most cases where changes to ScriptableObjects are not serialized is the wrong usage. ScriptableObjects are mainly meant to be stored as standalone assets in your project. If they should be referenced by prefabs you have to store them as asset. Creating an asset of a scriptable object is very easy nowadays. You can simple add the CreateAssetMenuAttribute attribute to the class(es) that you want to be able to create an instance of and you can simply create them from the Create menu in Unity.