I had a similar problem to this, where member variables were themselves class objects that inherited from each other. After a lot of researching and tinkering, I came up with a good solution for me. Here I am sharing it in the hope that it will help someone else. (And yes, I realize that this is an old post, but as it was at the top for my google searches . . .)
My goal was for Unity to show in the editor the specific stats class for each item category. In other words, I wanted a Weapon Object when selected to show WeaponStats, and an Armor Object when selected to show ArmorStats, all while Weapon and Armor had shared variables and WeaponStats and ArmorStats also had shared variables.
Specifically, I was trying to use the new keyword so that a base variable (of a class type) could be overridden by the derived class's variable (of a type derived from the base variable's class). As in:
public class ItemStats {
// variables like size, weight, etc
}
public class WeaponStats : ItemStats {
// Additional variables like damage, rate of fire, etc
}
public class ArmorStats : ItemStats {
// Additional variables like degree of damage reduction, etc
public class Item {
public ItemStats stats;
protected float degredation;
// etc.
}
public class Weapon : Item {
public new WeaponStats stats; // *The problem*
protected Ammo ammo;
// etc
}
public class Armor : Item {
public new ArmorStats stats; // *Same problem*
// etc
}
However, this didn't actually hide the base ItemStats variable and instead hid the new WeaponStats variable. As well as creating a lot of headache about what variable stats was referring to in the Weapon class.
tldr it was a bad idea.
The better solution I finally came up with, after learning a lot more about inheritance, was this:
// Make the base class abstract, so each item has its own subclass.
// This avoids having two variables for stats, the problem I was trying to avoid.
public abstract class Item {
public abstract ItemStats Stats();
// notice that there is no ItemStats variable here anymore
protected float degredation;
// etc.
}
public class Weapon : Item {
public WeaponStats stats;
protected Ammo ammo;
// This function lets other items interface with its stats.
public override ItemStats Stats() {
return stats;
}
// etc
}
// The same idea for Armor as for Weapon
This enabled Unity's Editor to only display the matching stats class (WeaponStats with Weapons and ArmorStats with Armor, for example); gave each Stats class inherited variables common to all ItemsStats; and also allowed all Items to know that other Items had stats.
Hope this is helpful to someone else :)