c# floating health bar

Hey i converted from java to c# i want to use for my enemy and anything i change keeps giving me error if any can help please thank you.

using UnityEngine;
using System.Collections;

public class AIHealth : MonoBehaviour {
public Transform HPtarget;

GameObject HPfabbie;

GameObject HPGUI;

FIXME_VAR_TYPE healthGUIWidth= 0.0f;

void  Start (){

HPGUI = Instantiate(HPfabbie,Vector3(0.5f,0.5f,0),Quaternion.identity)as GameObject;

healthGUIWidth = HPGUI.guiTexture.pixelInset.width;

}

void  Update (){

FIXME_VAR_TYPE mainCamera= FindCamera();

FIXME_VAR_TYPE p= mainCamera.WorldToViewportPoint(HPtarget.position+Vector3.up*1.5f);

FIXME_VAR_TYPE healthFraction= 0.5f;


HPGUI.guiTexture.pixelInset.xMax = HPGUI.guiTexture.pixelInset.xMin + healthGUIWidth * healthFraction;


HPGUI.transform.position = p;

}

void  FindCamera (){

    if (camera)

    return camera;

    else

    return Camera.main;

}
}

i was using this other script but the healthbar keeps showing behind other objects

using UnityEngine;
using System.Collections;

public class AIHealth : MonoBehaviour {
public float hp=100;
public float healthBarWidth;
public GameObject AIhealthBar;
public GameObject myHb;

	
void  Start (){		
 healthBarWidth=20f;
 myHb = Instantiate (AIhealthBar, transform.position, transform.rotation) as GameObject; 
}
 
void  Update (){
 // Health bar
 myHb.transform.position = Camera.main.WorldToViewportPoint(transform.position);
 myHb.transform.localScale = Vector3.zero;
 
healthBarWidth = -hp;
		
if( hp > 100){
	hp = 100;
		}
		
 myHb.guiTexture.pixelInset = new Rect(10,20,healthBarWidth, 10);

 
}
 
}

I’m guessing the error is that you have typed “FIXME_VAR_TYPE” instead of a type?

try replacing that with “var”, or the type that you want it to be.

Hah
Taken from the converter which you probably used
You have 2 wrong variable types (see FIXME_VAR_TYPE).
the hint is “FIXME

#region Imports

using UnityEngine;

#endregion

public class AIHealth : MonoBehaviour
{
    #region Public Fields

    public Transform HPtarget;

    #endregion

    #region Private Fields

    private GameObject HPGUI;
    private GameObject HPfabbie;


    private float healthGUIWidth;

    #endregion

    #region Private Methods

    private Camera FindCamera()
    {
        return camera ? camera : Camera.main;
    }


    private void Start()
    {
        HPGUI = Instantiate(HPfabbie, new Vector3(0.5f, 0.5f, 0), Quaternion.identity) as GameObject;


        if (HPGUI != null) healthGUIWidth = HPGUI.guiTexture.pixelInset.width;
    }


    private void Update()
    {
        Camera mainCamera = FindCamera();


        Vector3 p = mainCamera.WorldToViewportPoint(HPtarget.position + Vector3.up*1.5f);


        const float healthFraction = 0.5f;

        Rect pixel = HPGUI.guiTexture.pixelInset;
        pixel.xMax = HPGUI.guiTexture.pixelInset.xMin + healthGUIWidth*healthFraction;
        HPGUI.guiTexture.pixelInset = pixel;


        HPGUI.transform.position = p;
    }

    #endregion
}

I will thanks :slight_smile:

I fixed it for you…

Hey,

I used the code posted by Elite Mossy, but even though I have no immediate errors, when I start the game I receive this each frame :

Vector3 p = mainCamera.WorldToViewportPoint(HPtarget.position + Vector3.up*1.5f);

Also, where exactly in the script is the objects health referenced so I can put in my own health variable?

Thanks