Three of the four enemies works properly

[FIX]

I fixed it by changing line 2 from “private” to “public” then dragged in the player into the inspector.


EDIT: I fixed it. I found out what the problem was. I put the fix on top of the post in bold.

Not sure why that fixed it but it did.

Each enemy is not only set up the exact same, but the same script is on each one. Yet, one enemy refuses to follow you when you are in range of it, the other three follows you.

So the script is: enemy follows you when you are in range.

 public float speed; // how fast enemy runs at player
    private Transform target; //variable that holds the game object the enemy is chasing, in this case, Player 1
 
 


private void Start()
    {
        target = GameObject.Find("Player1").GetComponent<Transform>();

}



    private void Update()
    {
        if (Vector2.Distance(transform.position, target.position) < 5)
          
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);                                                                                       
                                                               

   }

It could be any number of things.

Are you getting any errors in your console?
Are all 4 enemies in the scene at the start of the game, or are some instantiated at runtime?

Yes, at the start of the game.

Here’s two errors I get in the console:

NullReferenceException: Object reference not set to an instance of an object.
NullReferenceException: Object reference not set to an instance of an object.

It brought me to line 9 and line 17 after clicking on those

[FIX]

I fixed it by changing line 2 from “private” to “public” then dragged in the player into the inspector.

Not sure why that fixed it but it did.