HealthBar Problem

How can I create health bar over a target or any GameObject?

there are different ways using OnGUI() or a GUITexture/Text.
the following script is is attached to your Player and is used to mark the enemy (or what ever) and to give a feedback of it´s energy/shield (or what ever).
the object, you what to “observe” in this way, needs to be tagged as “Enemy” (in this case) and it needs 2 scripts attached wich are called enemy_EnerCal and enemy_Attributes (all this names are up to you) . these scripts contain imporant vars:
curEnergy;
curShield;
EnemyName;
again, it´s up to you how many vars you will need. if you already know the GetComponent stuff, this is what happens:
enemyEnergy = enemies*.GetComponent<enemy_EnerCal>().curEnergy;*
enemyShield = enemies*.GetComponent<enemy_EnerCal>().curShield;
EnemyName = enemies.GetComponent<enemy_Attributes>().EnemyName;
///varxyzToYouWantToDisplay= enemies.GetComponent<enemy_Attributes>().varxyzBla;
anyway, if you have any trouble getting this to work, PM me. sure, this script is just 1 of a thousand ways to achieve this. is not perfect at all and needs to be optimized, but it works and will give you a nice start.
_```*_

*using UnityEngine;
using System.Collections;

public class player_Interface2 : MonoBehaviour {
//Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public bool clampToScreen = true; // If true, label will be visible even if object is off screen
//private float seeDistance = 150;
private float VisibleRange = 300;
private GameObject thePlayer;
private float currDistance;
public GUIStyle customGUI;
public GUIStyle customGUI2;
public Texture2D MarkerTextureNeutral;
public Texture2D MarkerTextureAggresiv;
private float ShieldBarWidth;
private float EnergyBarWidth;
//private float BarWidthFactor= 0.4f;
public Texture2D EnergyTexture;
public Texture2D ShieldTexture;
public bool neutral = true;
private GameObject enemies;
//private float targetRangeToEnemy = Mathf.Infinity;
private Vector3 screenPos;
public Camera cam;
private float clampBorderSize = 5;
//private Vector3 fwd = new Vector3(0,0,0);
private string EnemyName;
private float enemyEnergy;
private float enemyShield;
private bool objIsVisible;
//Enemy_Attributes enemy_Attributes;
/////////////////////////////
void Start (){
//cam = this.camera;
}
void Update (){
enemies = GameObject.FindGameObjectsWithTag(“Enemy”);
}
void OnGUI (){
GUI.depth = -10;
if(enemies != null){
for (int i = 0; i < enemies.Length; i++) {
//fwd = Vector3.MoveTowards(transform.position,new Vector3(enemies[i].transform.position.x, enemies[i].transform.position.y, enemies[i].transform.position.z),targetRangeToEnemy);
//if (Physics.Raycast (transform.position, fwd, targetRangeToEnemy)) {
// Debug.DrawRay(transform.position, fwd, Color.blue);
//}
if(clampToScreen) {
Vector3 relativePosition = cam.transform.InverseTransformPoint(enemies[i].transform.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);
screenPos = cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition/* + offset2/));
screenPos = new Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+MarkerTextureNeutral.width)),
Mathf.Clamp(screenPos.y,clampBorderSize,Screen.height-(clampBorderSize+MarkerTextureNeutral.height)),
screenPos.z);
//ist das objekt sichtbar
Vector3 visibilty = cam.WorldToScreenPoint(enemies[i].transform.position);
// Debug.Log(visibilty.z);
float screenwidth = Screen.width;
float screenheight = Screen.height;
if(visibilty.x < 0 || visibilty.x > screenwidth || visibilty.y < 0 || visibilty.y > screenheight || visibilty.z < 0)
objIsVisible = false;
else
objIsVisible = true;
}
else {
screenPos = cam.WorldToScreenPoint(transform.position/* + offset*/);
}
///////////////////////////////////////////////////////////////////////////
enemyEnergy = enemies[i].GetComponent<enemy_EnerCal>().curEnergy;
enemyShield = enemies[i].GetComponent<enemy_EnerCal>().curShield;
EnemyName = enemies[i].GetComponent<enemy_Attributes>().EnemyName;

		enemyEnergy = Mathf.Floor(enemyEnergy*100)/100;
		enemyShield = Mathf.Floor(enemyShield*100)/100;
		//enemy_Attributes = enemies[i].GetComponent<Enemy_Attributes>();
		//Enemy_Controller enemy_Controller = enemies[i].GetComponent<"Enemy_Controller">();
		///////////////////////////////////////////////////////////////////////////
		//EnergyBarWidth = enemy_EnerCal.EnergyPercent*BarWidthFactor;
		//ShieldBarWidth = enemy_EnerCal.ShieldPercent*BarWidthFactor;

// float nearToPlayer=0;
float distToPlayer;
distToPlayer = Mathf.RoundToInt(Vector3.Distance(enemies[i].transform.position,transform.position));
//Debug.Log(distToPlayer);
GUI.DrawTexture( new Rect(screenPos.x +1/- MarkerTextureNeutral.width/2/, Screen.height - screenPos.y-15, MarkerTextureNeutral.width, MarkerTextureNeutral.height),MarkerTextureNeutral);

		/*
		if (enemy_Controller.neutral){
			GUI.DrawTexture( new Rect(screenPos.x - MarkerTextureNeutral.width/2, Screen.height -screenPos.y, MarkerTextureNeutral.width, MarkerTextureNeutral.height),MarkerTextureNeutral);
		}
		if (!enemy_Controller.neutral){
			GUI.DrawTexture( new Rect(screenPos.x - MarkerTextureAggresiv.width/2, Screen.height -screenPos.y, MarkerTextureAggresiv.width, MarkerTextureAggresiv.height),MarkerTextureAggresiv);
		}
		*/
		//Debug.Log(enemies[i].renderer.isVisible);
		if (distToPlayer <= VisibleRange  objIsVisible){	
			//GUI.Label ( new Rect(screenPos.x, (Screen.height - screenPos.y) - 18, 200, 20), "Shield",customGUI2);
			//GUI.Label ( new Rect(screenPos.x, (Screen.height - screenPos.y) - 6, 200, 20), "Hull",customGUI2);
			GUI.Label ( new Rect(screenPos.x+38, (Screen.height - screenPos.y) - 18, 200, 20), enemyShield.ToString(),customGUI);
			GUI.Label ( new Rect(screenPos.x+38, (Screen.height - screenPos.y) - 6, 200, 20), enemyEnergy.ToString(), customGUI);
			GUI.Label ( new Rect(screenPos.x -43, (Screen.height - screenPos.y) -30, 200, 20), EnemyName, customGUI);
			//GUI.Label ( new Rect(screenPos.x -43, (Screen.height - screenPos.y)+4, 200, 20), distToPlayer.ToString() + " meters",customGUI);
			//GUI.DrawTexture( new Rect(screenPos.x - 43,(Screen.height - screenPos.y) - 14,ShieldBarWidth,7), ShieldTexture);
			//GUI.DrawTexture( new Rect(screenPos.x - 43,(Screen.height - screenPos.y) - 5,EnergyBarWidth,7), EnergyTexture);
		}
	}
}

// else return false;
}
//screenPos = camera.WorldToScreenPoint (enemies[i].transform.position);
// if(clampToScreen) {
//screenPos.x = Mathf.Clamp(screenPos.x, Screen.width-Screen.width+10, Screen.width-10);
//screenPos.y = Mathf.Clamp(screenPos.y, Screen.height-Screen.height+20, Screen.height-20);
//screenPos.z = Mathf.Max(0.5f,this.transform.localPosition.z+1);

}
_```*_