Vector3.distance is always returning 0

Hello,
Im trying to make an enemy follow my player by flying towards him al the time. And I want to use vector3.distance to make the enemy stop a short distance before the player. But it keeps returning 0,0,0 on both my player and enemy gameobjects. Here is my code so far.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemySkullMovement : MonoBehaviour {
    
    	public 	Vector3 target;
    	public 	float 	speed;
    	private float 	distance;
    	private Vector3 player;
    	private Vector3 enemy;
    
    	void Start()
    	{
    		player = GameObject.FindGameObjectWithTag("Player").transform.position;
    		enemy = GameObject.FindGameObjectWithTag("Enemy").transform.position;
}
    
    	void Update() {
    		
    		Look();
    		distance = Vector3.Distance(enemy, player);
    		target = GameObject.FindGameObjectWithTag("Player").transform.position;
    		float step = speed * Time.deltaTime;
    		print(distance);
    		Debug.Log(player);
    		Debug.Log(enemy);
    		Debug.Log(distance);
    		if(distance > 1 && distance != 0) 
    		{
    			transform.position = Vector3.MoveTowards(transform.position, target, step);
    		}		
    	}
    
    	void Look(){
    		transform.LookAt(GameObject.FindWithTag("Player").transform);
    	}    
    }

Any help will be greatly apprreciated.

You seem to compare “enemy” and “player” values in Vector3.Distance().

— But you never update them! —
You only update “target.position”.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 public class EnemySkullMovement : MonoBehaviour {
 
     public     float     speed;
     private float     distance;
     private Transform target;

 
     void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player").transform;
         // the enemy is this GameObject so access to "transform" means access to enemy.transform
}

     void Update() {
         Look();
         distance = Vector3.Distance(transform.position, player.position);
         float step = speed * Time.deltaTime;
         print(distance);
         Debug.Log(target.position);
         Debug.Log(transform);
         Debug.Log(distance);
         if(distance > 1.0f && distance != 0) 
         {
             transform.position = Vector3.MoveTowards(transform.position, target.position, step);
         }        
     }

     void Look(){
         transform.LookAt(target);
     }    
 }

Try this.

Hi @Jaapster1337:

To me the script is looking fine but my doubt is that the “Player” or "Enemy " or both of them is null so look to console and check whats the the result of the Debug statement is, if its not showing any thing or there is any error about null reference then its means that player or enemy is null.

and i will suggest not to use the “FindGameObjectWithTag” due to sometime we forget to tag our object in game or some time spelling mistake happens when tagging object and also why to wast little CPU power to find the game object instead we can cache these objects.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySkullMovement : MonoBehaviour
{
    public  float speed;
    private float distance;

    private float step;
    private Transform t;
    private Transform target;


    void Start()
    {
        t      = transform;
        target = GameObject.FindGameObjectWithTag( "Player" ).transform;
    }

    void Update()
    {
        Look();
        CheckDistance();
        step = speed * Time.deltaTime;

        // Distance exist?
        if ( distance != 0 && distance > 1 )
        {
            t.position = Vector3.MoveTowards( t.position, target.position, step );
        }
    }

    void Look()
    {
        t.LookAt( target );
    }

    void CheckDistance()
    {
        distance = Vector3.Distance( t.position, target.position );
        Debug.Log( distance );
    }
}