Enemy not moving to updated Player position if i use a Player Prefab

Hi,
i have some trouble understanding the difference between assigning a Gameobject via drag&drop vs using Gameobject.Find

i have an abstract Enemy class, which looks like this:

public abstract class Enemy : MonoBehaviour
{
    public float speed = 5;
    public GameObject player;

    public void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position, player.transform.position, Time.deltaTime*speed);
    }

I assign the player Gameobject in the Unity editor (i have to make a Prefab of the Player to be able to assign it with drag&drop)

the Child Class EnemyEasy calls the Move() method in its Update method.

The Enemy Gameobject as the EnemyEasy script attached to it.

This is the result:

The Enemy Gameobject moves towards the starting position of the player.
but as soon as the player moves away, the Enemy wont follow him.

Why is that?

If I use player = GameObject.Find("Player"); in the Start() method of the abstract class, everything works fine. The enemy follows the player, even if the player moves away.

But if i assign the Player Prefab Gameobject via drag&drop, the enemy just moves to the starting position of the player.

I’m guessing, you assign Player from the Project window. In this case Prefabs are not runtime objects, they are just “templates” for the gameobjects on the scene. They have only default components with default values, e.g. position/rotation/scale. That’s why it works for you at start, the enemy moves to the default position for the prefab.

When you assign prefab from Project window, you link it with the file in your project, not with the object in the actual game at runtime. Usually, this way prefabs are used to Instantiate() gameobjects only, and then you work with the instantiated gameobjects, a copy of the prefab.

GameObject.Find() searches for gameobject in your current scene, which exists at runtime and that’s why it works, you get a link to the runtime gameobject (as if it was a copy of the prefab)

So, either you need to Instantiate() your Player prefab and work with the resulting gameobject or you need to place Player prefab on the scene and assign it to the Enemy dragging the object from the Hierarchy window.