Inheritance suddenly stopped working.

I have an enemy parent with the following code:

  public class Enemy_Parent : MonoBehaviour
{
    [SerializeField]
    public Player _player;
    public int _resistance;
    public int _level;

    void Start()
    {
        _player = GameObject.Find("Player").GetComponent<Player>();
        print(_player);
    }

    public enum _enemy_resistance
    {
        Fire_1 = 0,
    }
}

When I run it, the console displays “Player”, which confirms that “Player” is found. Additionally, since it is a serializedfield the object also populates in the inspector under the enemy parent script.

I then have an enemy creature with the following code.

public class Creature : Enemy_Parent

In the Start function I have:

void Start()
{
    print(_player);
}

However, Null is displayed in the console. Since I typed “public class Creature : Enemy_Parent” shouldn’t my creature enemy automatically see it. Before I went to sleep last night everything was running fine. Today I launch Unity and I’m receiving errors because “_player” is null.

When you call void Start()from Creature, the line _player = GameObject.Find("Player").GetComponent<Player>();from Enemy_Parent’s Start function won’t get called.

Personally, I prefer to keep Start() completely empty within base classes since they won’t get called by inherited classes. If you really want to call Start() of base class, set base class’ Start() to a virtual method. It would look like this;

virtual void Start()
{
	_player = GameObject.Find("Player").GetComponent<Player>();
	print(_player);
}

Start function in your inherited class would look like this then.

override void Start()
{
	base.Start();
	print(_player);
}

Make sure base.Start(); comes before any code so that whatever code you have in your parent class runs first.

Then is may be best to keep Enemy_Start empty. I may try the override, but my understanding of parent/child is lacking. I thought I was doing okay too!

Since Creature is a child of Enemy_Parent, I do not need to code: private Enemy_Parent _enemy_parent within Creature. Is that correct?

I was basically trying to store _player once, so it could be accessed from any enemy child I add. If I add it to all enemy child Starts by overriding and accessing from parent, would that be considered overkill? Or even if I call upon an Enemy_Parent function from a child to obtain _player.

I’m not sure how to move forward in the most efficient way. It is possible to store _player like I planned? Any guidance would be appreciated.