Making a Health Bar with Code

I have a damage reciever script, and a health bar script, but I want the health bar to decrease as the player gets hit. How should I do that? Here are my scripts:

Health Bar GUI Script

function OnGUI() {
    GUI.backgroundColor = Color.green;
      GUI.Button(Rect(10,640,510,20),"Health");
}

Damage Reciever Script

var hitPoints = 100.0; 
var detonationDelay = 0.0; 
var explosion : GameObject; 
var deadReplacement : Rigidbody; 

function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;

hitPoints -= damage;
if (hitPoints <= 0.0) {
    // Start emitting particles
    var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
    if (emitter)
        emitter.emit = true;

    Invoke("DelayedDetonate", detonationDelay);
}
}

function DelayedDetonate () { BroadcastMessage ("Detonate"); }

function Detonate () { // Destroy ourselves Destroy(gameObject);

// Create the explosion
if (explosion)
    Instantiate (explosion, transform.position, transform.rotation);

// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
    var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

    // For better effect we assign the same velocity to the exploded barrel
    dead.rigidbody.velocity = rigidbody.velocity;
    dead.angularVelocity = rigidbody.angularVelocity;
}

// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
// right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
    emitter.emit = false;
    emitter.transform.parent = null;
}
Destroy (gameObject);
}

// We require the barrel to be a rigidbody, so that it can do nice physics @script RequireComponent (Rigidbody)

The damage reciever script works. All I need is the damage done using the damage reciever to affect the health bar (shorten the width). Thanks in advance!

It is add in to the Damage Receiver Script.

function OnGUI()
{
    GUI.HorizontalScrollbar(Rect (0,40,200,20), 0, hitPoints,0, 100);
}

There are seriously tons of posts on this, with examples for rectangular health bars, circular health bars, anything you could imagine.

Here’s the top Google result for ‘Unity Health Bar’:

http://answers.unity3d.com/questions/17255/how-do-i-make-a-health-bar.html

Next time, check around at least a little bit before posting a question, because otherwise folks like me get grumpy.

Not sure if you were only looking to narrow the width…

var scale = 1.0f;

function OnGUI() {
    GUI.backgroundColor = Color.green;
    GUI.Button(Rect(10, 640, 510 * scale, 20), "Health");
}

// Call this to update the health.
// Note: 100.0f should be max.
function SetHealth(health : float) {
    scale = Mathf.Clamp01(health / 100.0f);
}

yeah, that's my issue, I don't know how to link the two scripts so the health bar goes down.

Alrighty, that's a very reasonable question to ask.

There are a number of ways to link scripts. If you want to do it by hand, create a public variable of type [name of script] and then drop your other script into it in the editor. You can then access public functions in the referenced script like so

otherCode.doSomething();

where otherCode is your reference to the other script and doSomething is your public function.

You can also look into GameObject.Find to get a reference to the game object containing the other function, and then GetComponent to access the script. There are also more advanced things like FindObjectOfType.

Finally, if you just want to send a message to a script in a game object without worrying about what the receiving script is called, you can use SendMessage. Don't know how to use SendMessage, and the script reference is confusing? There are quite a few notes on it here and in the forums.

Best of luck,

Julien

Here is the solution :

var Health = 100;
var position = Rect (500,15,200,20);

function OnTriggerEnter (Objecte : Collider) {
	if (Objecte.tag == "Obstacle") 
	{
		Health -= 50;
		
	}
}

function Update ()
{
	if (Health <= 0)
	{
		Application.LoadLevel (Application.loadedLevel);
	}
}

 function OnGUI()
{
    GUI.backgroundColor = Color.green;
    GUI.HorizontalScrollbar(position, 0, Health,0, 100);
}