- Home /
Using a Derived property on a Derived Class
Either I didn't found what I will ask or I didn't knew how to find it. What I want to do is the following:
public class BaseData
{
// Whatever data here
}
public class DerivedData : BaseData
{
// Whatever other data here
}
public class BaseClass : MonoBehaviour
{
private BaseData _data;
public BaseData Data
{
get{ return _data;}
set{ _data = value;}
}
}
public class DerivedClass : BaseClass
{
public new DerivedData Data
{
get{ return base.Data as DerivedData;}
set{ base.Data = value;}
}
}
The reason I want to do that is so that whenever I have some data on an object and I want to serialize it to a file, I would just serialize that single _data, and also so I could access the correct data on every derived class without having on every derived class a separate Data property.
So, would the _data behave correctly even if I derived several other classes, always storing the correct object or will it have issues doing that?
I'm also open to whatever other way to do that same thing.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Loading a Constant Array Of Integers 0 Answers
Debug.Log a gameobject in C ? 2 Answers
How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 Answers