- Home /
Inheritance problem ?
Hello,
Recently I updated a lot of outdated lines of code in my project, but now I got an inheritance problem, and I don't know how to fix it :
I declare a CharacterStatSheet object in my Character class ( abstract class ), this object is set in childs of this class ( monster / player )
In Monster class the CharacterStatSheet is supposed to be a MonsterStatSheet object, but it seems like it's not the case it's a CharacterStatSheet object.
( here is the lines )
public abstract class Character : MonoBehaviour {
public CharacterStatSheet charStatSheet;
protected virtual void Reset()
{
[...]
}
[...]
}
public class Monster : Character {
protected override void Reset()
{
charStatSheet = new MonsterStatSheet();
}
}
Any idea ?
Answer by Bunny83 · Jan 15, 2019 at 12:32 AM
Unity's serializer does not support inheritance for custom serializable classes. Please read the script serialization documentation page carefully. Especially the section labelled
When might the serializer behave unexpectedly?
Polymorphism is only supported for MonoBehaviour or ScriptableObject derived types. Custom serializable classes behave like structs and are serialized inline based on the variable / field type. Since your field has the type "CharacterStatSheet" it will always be serialized as CharacterStatSheet, no matter what you assign to this field.
If you want to better understand how Unity serializes your objects, i recommend setting your asset serialization mode to "force text" (which i think is now already the default) and open the serialized asset (prefab or scene file) in a simple text editor. An editor that has YAML highlighting would be recommended.
You may also want to look through the sub categories of the text-based scene files which roughly explain the YAML format and provide a list of type IDs which makes it easier to understand and navigate such a YAML file. If you inspect your serialized object you will notice that custom serializable classes do not store any type information. The serializable fields are just serialized as sub fields.
Answer by toomasio · Jan 14, 2019 at 06:08 PM
you could do something like
public abstract class Character : MonoBehaviour
{
protected CharacterStatSheet charStatSheet;
public CharacterStatSheet CharStatSheet { get { return (CharacterStatSheet)charStatSheet; } }
protected virtual void Reset()
{
[...]
}
[...]
}
then
public class Monster : Character {
public new MonsterStatSheet CharStatSheet { get { return (MonsterStatSheet)charStatSheet; } }
protected override void Reset()
{
//dont really need this anymore you can just get the public CharStatSheet
//charStatSheet = new MonsterStatSheet();
}
}
This should work if MonsterStatSheet is a member of CharacterStatSheet
It's not working :( ( Cannot cast from source type to destination type )
I'll explain a little bit more my problem.
$$anonymous$$onsterStatSheet inherits from CharacterStatSheet, methods are same, but the $$anonymous$$onsterStatSheet add a new Stat Object ( called SpawnRate ) ... the problem is that it isn't showing up in the inspector, and my method to get the spawn rate returns null, so it's not instantiated.
Answer by badadam · Jan 14, 2019 at 07:21 PM
MonsterStatSheet must inherits from CharacterStatSheet. Your charStatSheet is instance of CharacterStatSheet and you assign MonsterStatSheet instance. When you run charStatSheet instance's visual methods, the methods which is overided by MonsterStatSheet class run. But If a method of MonsterStatSheet class is not overrided method of CharacterStatSheet, you can't call this from CharacterStatSheet class' instance.
$$anonymous$$onsterStatSheet inherits from CharacterStatSheet. Currently I got a new Stat Object added in the $$anonymous$$onsterStatSheet class, but it's not showing up in the inspector.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Is it possible to change value on base class when the same field gets changed on it's derived class 0 Answers
How to prevent an inherited variable from showing up on a scriptable object? 1 Answer
Unity exports my derived class, but not its base class (C# OOP) - Is there a workaround? 0 Answers