Why doesn't this AI script work? c#

hey there,

So I’m creating a object that moves back and forth from 2 points, i wanted to make it if the player touched the gameobject it loses health.

I made an empty gameobject with 3 different spheres attached to it.

Here is the code:

using UnityEngine;
using System.Collections;

public class Enemy2AI : MonoBehaviour {
	public Transform StartPoint;
	public GameObject Player;
	public Transform Endpoint;
	public bool DirectionSwap; 
	public float Speed;
	
	void FixedUpdate () {		
	if(transform.position == Endpoint.position){
	DirectionSwap = true;		
	}
	if(transform.position == StartPoint.position){
	DirectionSwap = false;		
	}	
		
	if(DirectionSwap){
	transform.position = Vector3.MoveTowards(transform.position,StartPoint.position,Speed);
	}
	else{
	transform.position = Vector3.MoveTowards(transform.position, Endpoint.position,Speed);		
		}
	
	if(Player.transform.position == gameObject.transform.position){
	HealthBar hb = Player.GetComponent<HealthBar>();
	hb._Currenthealth -= 10;					
	}
		
		
	}
}

Two vectors in 3D space consisting of 3 floats each will almost never be exactly equal.

Instead, attach colliders to the player and the object.

In a script on one of the objects, include:

void OnCollisionEnter(Collision collision) {
    HealthBar hb = Player.GetComponent<HealthBar>();
    hb._Currenthealth -= 10; 
}

Reference: Unity - Scripting API: Collider.OnCollisionEnter(Collision)