Health script

Well, I have health script but I want GUI that says how many health is left (GUI box and numbers of your health, not health bar)

Current script:

var health : float = 100f;
var damagePerShot : float = 50f;

function OnTriggerEnter (other : Collider) {

if (other.gameObject.tag == "Bullet") {
	health -= damagePerShot;
	Destroy(other.gameObject);
	}
}

function Update() {
	if (health <= 0) {
	health = 100f;	
	transform.position.x = 15;
	transform.position.y = 2.5;
	transform.position.z = 28.5;
	transform.rotation.x = 0;
	transform.rotation.y = 180;
	transform.rotation.z = 0;
	}
}

function OnGUI() {
	GUI.Box(Rect(0,0,100,100), health);
}

This isn’t working, please help, thanks :slight_smile:

I’m pretty sure that you have to use a string instead of a float, so you can type in the OnGUI function, var stringHealth : string = health.ToString();
Then replace health with stringHealth

So it’d be

var health : float = 100f;

var damagePerShot : float = 50f;

 

function OnTriggerEnter (other : Collider) {

 

if (other.gameObject.tag == "Bullet") {

    health -= damagePerShot;

    Destroy(other.gameObject);

    }

}

 

function Update() {

    if (health <= 0) {

    health = 100f;  

    transform.position.x = 15;

    transform.position.y = 2.5;

    transform.position.z = 28.5;

    transform.rotation.x = 0;

    transform.rotation.y = 180;

    transform.rotation.z = 0;

    }

}

 

function OnGUI() {
   var stringHealth : string = health.ToString();

    GUI.Box(new Rect(0,0,100,100), stringHealth);

}

EDIT: You need to change Rect to"new Rect"