What is wrong with this code ?

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);
     
    }
     
    }

The combination of these lines is creating the problem:

var wantedPos = Camera.main.WorldToViewportPoint (target.position);
transform.position = wantedPos; 

...

//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)
);

The calculation, setting the transform.position to a viewportPoint, doesn’t makes sense to me. I clean up some of the lines, attached the script to a simple cube, and I got a working health bar right under the cube.

using UnityEngine;
using System.Collections;

public class AIHealth : MonoBehaviour {
	public int maxHp;
	public int currentHp;
	public Texture2D hpBarBG;
	public Texture2D hpBarCurrent;
	
	private float offsetY;
	
	void Start () {
		offsetY=gameObject.collider.bounds.size.y-2;
	}
	
	void Update () {
		//I commented these 3 lines out when I tested it
		health h=(health)GetComponent("health");
		h.currenthealth = currentHp;
		h.maxhealth = maxHp;
		
		//Removed the 2 line here, they are screwing things up
	}
	
	void OnGUI(){
		Rect rcBg = new Rect(0,0,60,14);
		
		//Fixed this: added in the 1.0f to correct the calculation
		Rect rcCurrent = new Rect(0,0,((currentHp)*1.0f/maxHp)*56,8);
		
		//Just a simple clean up
		Vector3 point = Camera.main.WorldToScreenPoint(
			transform.position + Vector3.up * offsetY
		);
		
		//Everything else are the same from here on
		...
	}
}