Hi I am trying to create a game where there are different types of zombies. I decided to create a base class for the zombies that includes methods for moving and flipping characters and then I have individual classes for each type of zombie where I will modify things like health and damage.

I made it so the zombie prefab I put in the game has a script that inherits from the Zombie class but when I run the game nothing in the Zombie.cs script is run.

Anyway, here are my classes:


public class Zombie : MonoBehaviour {

    //zombie goes after this target
    Transform targetPlayer; 

    //Zombie Rigid Body
    Rigidbody2D zombieRB; 

    //The speed the zombie will move
    public float moveSpeed;

    //Used for flipping zombie based on which way it is going
    bool facingRight = true;

    // Use this for initialization
    void Start () {

        zombieRB = GetComponent <Rigidbody2D> ();

        //This finds the player object to follow it
        targetPlayer = GameObject.FindGameObjectWithTag ("Player").transform;

    // Update is called once per frame
    void Update () {

        //Checks zombies position in comparison to player to see where it should move.
        if (transform.position.x < targetPlayer.position.x) {
            //zombie to the left of target go right

            if (!facingRight) { //Check if needs flipping
                flipZombie ();

            zombieRB.velocity = new Vector2 (moveSpeed, zombieRB.velocity.y);

        } else {
            //zombie to the right of target go left

            if (facingRight) { //Check if needs flipping
                flipZombie ();

            zombieRB.velocity = new Vector2 (moveSpeed * -1, zombieRB.velocity.y);


    /// <summary>
    /// Flips the zombie when moving
    /// </summary>
    void flipZombie() {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;

And then I have a pulserZombie.cs but there is nothing in it but the inheritance:

public class pulserMovement : Zombie {

Here is my image of the prefab I have:

enter image description here

  • \$\begingroup\$ Subclassing is usually not the best method to create variations in Unity. Because of the way the editor is set up, you'll get a lot more mileage out of varying your zombies using prefabs with 1) different inspector property values (as you're doing for move speed) and 2) different combinations of components. (eg. A "pulse" zombie has a pulse movement component, rather than is a pulseMovement subclass of zombie.) This often ends up being more flexible as you iterate on your game. See the principle of composition over inheritance \$\endgroup\$
    – DMGregory
    Sep 8, 2016 at 3:46
  • \$\begingroup\$ To compliment @DMGregorys suggestion, I have found bugs in inheritancy and the Unity inspector, where inheritancy is lost because 'Unity forgot it was inheriting'. Im told this is because the editor side is not set up for inheritancy, thus immediately breaking the link. \$\endgroup\$
    – Gnemlock
    Sep 8, 2016 at 4:08

1 Answer 1


By not specifically setting your methods as public, your methods are set to private. They should be set to protected.

When we inherit, we lose access to all private methods and variables. We use protected to say "this is still private from the outside, but it is still accessible through inheritancy".

Note that if you intend to add any extra functionality to a method in your child class, you will also need to set it as virtual, with the child class referencing it as override. You can than write a new method, using base.methodName to call the base version of the method, if needed.

virtual protected void Update() {...}

override protected void Update()


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .