Struggling to implement inheritance

Hey all,

So I have an Enemy script from which all the enemies that I create are supposed to inherit from. I did this so that I would not have to rewrite all the code that is common between them (there will be a lot). However, I feel like I’m doing something wrong here.

Here is the parent Enemy Class

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class enemy : MonoBehaviour  {
    public int health;   //
    public int damage;   // enemy
    public int value;    // attributes
    public float speed;  //

    public Slider hpSlider;
    public Text hpText;

    NavMeshAgent agent;
    public Transform target;

    public GameObject gameControl; //cached GameControl
    void Awake()
    {
        gameControl = GameObject.FindGameObjectWithTag("GameController"); //cache the GameControl
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(target.position);
    }

    void Start()
    {
        hpSlider.maxValue = health;
        hpSlider.value = health;
        hpText.text = health.ToString();
    }

    public void death() //death stuff
    {
        gameControl.GetComponent<GameControl>().addPoints(value); //reward with points
        Destroy(gameObject); //get rid of enemy
    }

    void Update()
    {
        if(target.tag != "CPU")
        {
            agent.SetDestination(target.position);
        }
       
    }

    void takeDamage(int inDamage)
    {
        health -= inDamage;
        hpSlider.value = health;
        hpText.text = health.ToString();
    }

}

and then I have a basic enemy with nothing much in it:

using UnityEngine;
using System.Collections;

public class enemy_basic : enemy {

    // Use this for initialization
    void Start () {
        this.health = 50;
        this.speed = 10;
       this.value = 5;
       this.damage = 5;
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

yet I’m finding that stuff like setting the UI element to my current health (as seen in Start() in the parent function) do not work on the basic enemy.

I figured that since it is in the parent Start() function, it should run at start in any object that inherits from enemy.cs

What am I missing here? or am I using inheritance wrong?

How are you using the classes? e.g. can you paste an example of code that accesses these members?

Start for instance is being overridden by your enemy_basic class. The parents start is never called. Use base.Start() to call the parent function.

You could also do something like this in the base class:

public class Enemy : MonoBehaviour
{
   [SerializeField]
    private Text hpText;

    private int m_health;

    protected int Health
    {
        get
        {
            return m_health;
        }
        protected set
        {
            m_health = value;
            hpText.text = m_health.ToString();
        }
    }
}

Then when your child classes update their health, the text automatically gets updated aswell.