Is it possible for a Scriptable Object to have a field of a specific type, and have that type be determined based on the type of another field? For example:
public class ItemObject : ScriptableObject
{
public ItemType type;
public Item data = new Item();
}
ItemType is an enum
public enum ItemType
{
Physical,
Magical
}
Item is a basic class, with 2 other classes inheriting from it, MagicalItem and PhysicalItem.
[System.Serializable]
public class MagicalItem : Item {
public string passive;
public MagicalItem() {
passive = "";
}
}
ect.
I'd like ItemObject to be able to choose in the editor from either of these options, or many more as I add new item types to my game. I haven't been able to figure out a solution but my thought is to have the data type be set on Awake() based on the type, but I haven't been able to get that to work. IE something like this:
private void Awake() {
if(type == ItemType.Magical) {
data = new MagicalItem();
} else {
data = new PhysicalItem();
}
}
Is there a better way to solve this problem?
Thanks!
