Hi I got a Parent class “Building” which has a property named “buildings” i got a child of this class named “SpecificBuilding” which also has the property “building” and hides the property of the parent. Now i got a dictonary which loks something like this: public Dictonary<int,Building> someDictonary = new();
if i now acces the diconary and get the entry which reffers to the “SpecificBuilding”-Class and access the “buildings”-property, it will reffer to the property of the parent class because I dont know how set the values of the dictonary to only children of the parent class so that if i get the value i wont reffer to the parent-class-property but the child-class-property.
If you have any Ide on how to solve my Problem i would be exited to hear your answers.
Normally you want to avoid hiding properties. MonoBehaviours will trigger an error like The same field name is serialized multiple times in the class or its parent class. This is not supported if you try. You can override getters if you want to access child properties e.g.
public class Building : MonoBehaviour
{
public string buildings = "from Building";
public virtual string Buildings => buildings;
}
public class SpecificBuilding : Building
{
public new string buildings = "from SpecificBuilding"; // will cause a serialize error
public override string Buildings => buildings;
}
public class BuildingTest : MonoBehaviour
{
public List<Building> testBuildings;
void Start()
{
foreach(var building in testBuildings)
{
// "from Building - from SpecificBuilding"
Debug.LogError(building.buildings + " - " + building.Buildings);
}
}
}