I have a base abstract class Hero with a protected int health in it.
I have a second class Mage which inherits Hero. I want to set a health value for the mage.
health = 5; → not working, it says its a field but is used as a type
What do i do? How do i set values for all the types of heroes?
You can’t override the health value like that. You have to set a different value inside some function, like Awake for example:
public abstract class Hero : MonoBehaviour {
protected int health;
}
public class Mage: Hero {
void Awake() {
health = 10;
}
}
public class Archer: Hero {
void Awake() {
health = 15;
}
}
holy smokes! you should set value in the constructors! like this:
Child class:
public Mage ()
{
health = 10;
}