I have a Person MonoBehaviour class:
public class Person : MonoBehaviour {
public string name;
public Animal pet;
}
An serializable abstract Animal class:
[System.Serializable]
public abstract class Animal {
public string name;
public virtual void Run () {}
}
Also a Cat class inherited from Animal
[System.Serializable]
public class Cat : Animal {
public string catType;
public override void Run () {Debug.Log ("Cat Run");}
}
Then in editor I cannot directly assign the pet to a person:
However, if I specifically assign Cat to Person, it works:
public class Person : MonoBehaviour {
public string name;
public Cat pet;
}
But that isn’t what I want: e.g. Tom always has Cat as pet, while Peter can have Dog
The only way I can think of is making Animal a MonoBehaviour, create a prefab, attach the Cat to the prefab, and finally drag the prefab to Person.
This is also not preferable because Animal doesn’t need to be MonoBehaviour and this can create a lot of prefabs.
Can this be solved by using custom editor? So I can drag the Cat script to Person, then the editor know the pet is a Cat type and display the Cat variables, etc.