Quick check about what is wrong with the script

Hereunder is he health bar code I have for my units (a green bar floating above the units - RTS style). The problem is that the bar does not stick to the unit (or move together with it). In the second i move the camera the health bar moves in that direction.

Can you please help ?

    using UnityEngine;
    using System.Collections;
     
     
    public class AIHealth : MonoBehaviour {
    public int maxHp;
    public int currentHp;
    public Camera mainCamera;
    public Texture2D hpBarBG;
    public Texture2D hpBarCurrent;
    public Transform target;
     
    private float offsetY;
     
    //script will be atached to each GameObject that can suffer dmg.
    void Start () {
    //put it on top of the character
    offsetY=gameObject.collider.bounds.size.y-2;
    Debug.Log(offsetY);
    }
     
    void Update () {
    health h=(health)GetComponent("health");
    h.currenthealth = currentHp;
    h.maxhealth = maxHp;
    var wantedPos = Camera.main.WorldToViewportPoint (target.position);
    transform.position = wantedPos;
    }
     
    void OnGUI(){
    //the hp bar background location on screen
    Rect rcBg = new Rect(0,0,60,14);
    //the current HP bar
    Rect rcCurrent = new Rect(0,0,((currentHp)/maxHp)*56,8);
     
    //the important part : where this object is located in my screen
    Vector3 point = mainCamera.WorldToScreenPoint(new Vector3(
    transform.position.x,
    transform.position.y+offsetY,
    transform.position.z)
    );
     
    //had to add the substraction since my Y axis were revesed (
    rcBg.y = Screen.height-point.y;
     
    //do some ugly hardcoding so the bar is pretty and centered
    rcBg.x = point.x-30;
    rcCurrent.x=rcBg.x+2;
    rcCurrent.y=rcBg.y+3;
    GUI.DrawTexture(rcBg,hpBarBG);
    GUI.DrawTexture(rcCurrent,hpBarCurrent);
     
    }
     
    }

Hi!
I tried to run this code and faced with some issues. So I rewrote it partly, and this solve some problems.
Try this:

using UnityEngine;

public class Damageable : MonoBehaviour
{
    private float maxHp = 100;
    private float currentHp = 100;
    private Texture2D hpBarBG;
    private Texture2D hpBarCurrent;

    /// HealthBar size
    public Vector2 size = new Vector2(50, 5);
    public float offset_Y = -35;

    public void Damage(float damage)
    {
        currentHp -= damage;
    }

    void Start()
    {
        /// Generate HealthBar backward texture
        if (hpBarBG == null)
        {
            Texture2D t = new Texture2D(1, 1);
            t.SetPixel(0, 0, Color.black);
            t.Apply();
            hpBarBG = t;
        }

        /// Generate HealthBar forward texture
        if (hpBarCurrent == null)
        {
            Texture2D t1 = new Texture2D(1, 1);
            t1.SetPixel(0, 0, Color.green);
            t1.Apply();
            hpBarCurrent = t1;
        }
    }

    void OnGUI()
    {
        if (Camera.current != null)
        {
            Vector3 point = Camera.current.WorldToScreenPoint(transform.position);
            GUI.DrawTexture(new Rect(point.x - size.x / 2, Screen.height - point.y + offset_Y, size.x, size.y), hpBarBG);
            GUI.DrawTexture(new Rect(point.x - size.x / 2, Screen.height - point.y + offset_Y, currentHp / maxHp * size.x, size.y), hpBarCurrent);
        }
    }
}

Add this to any GameObject, and when you want to decrease health bar value, just call SendMessage method (from any other script on this object):

SendMessage("Damage", 10, SendMessageOptions.DontRequireReceiver);