GUI Health Bar

var h1 : Texture2D;
var h2 : Texture2D;
var h3 : Texture2D;
var h4 : Texture2D;
var h5 : Texture2D;
var h6 : Texture2D;
var h7 : Texture2D;

static var HEALTH = 70;

function Update() 
{
	var g_Health = GameObject.Find("fullhp");

	if(HEALTH > 60)
	{
	    g_Health.guiTexture.texture = h7;

	    return;
	}

	else if(HEALTH > 50)
	{
	    g_Health.guiTexture.texture = h6;

    	return;
	}

	else if(HEALTH > 40)
	{
	    g_Health.guiTexture.texture = h5;

	    return;
	}

	else if(HEALTH > 30)
	{
	    g_Health.guiTexture.texture = h4;

	    return;
	}

	else if(HEALTH > 20)
	{
	    g_Health.guiTexture.texture = h3;

	    return;
	}

	else if(HEALTH > 10)
	{
	    g_Health.guiTexture.texture = h2;

	    return;
	}

	else if(HEALTH <= 0)
	{
	    g_Health.guiTexture.texture = h1;

	    return;
	}

}


//InvokeRepeating("subtract", 2, 0.5);

//function subtract()
//{
//	HEALTH -= 1;
//	print("patay" + HEALTH);
//}

this the codes i get from a certain tutorial…its working fine.the problem is that i want the GUI healt bar to decrease after being hit by a spear…tnx in advance…anyone please do it in javascript…

Hi there, I won’t write the script, but help you to learn what is happening and how to add this for yourself =]

Let’s break it down. So you want to change the static variable HEALTH every time a spear hits the object with this script. How does that work in your commented part of the script? They call the function Subtract, and in there it says HEALTH -= 1;

Let’s see if we can do that first by using the space bar.

First let’s make a function to subtract the health. Just to make a point, function names are always started with a capitol letter :

function SubtractHealth()
{
	HEALTH -= 1;
	Debug.Log( "patay" + HEALTH );
}

Now to call the function using Input.GetKeyDown : Unity - Scripting API: Input.GetKeyDown

Put this in the start of the Update function :

    if ( Input.GetKeyDown(Keycode.Space) )
    {
        SubtractHealth();
    }

you should be happy to see that every time you tap the space, the health is reduced. But now instead of pressing the space bar, it should happen when a spear collides with the object. I think this is much better explained with a video : Unity 3D Student - 3d Modelling

And the script reference : Unity - Scripting API: Collider.OnCollisionEnter(Collision)

So from that we get a function like :

function OnCollisionEnter( theCollision : Collision )
{
    if(theCollision.gameObject.name == "Spear")
    {
        SubtractHealth();
        Debug.Log( "Hit by a Spear !" );
    }
}

If all is going well then you should now have your script working, as long as you change .name == “Spear” to what your spear is called.

I just want to show you one more trick, so you can subtract different amounts of health, not just -1.

Lets go back to using the keys for an example

put the input space back in the script, and add this after it :

    if ( Input.GetKeyDown(Keycode.5) )
    {
        SubtractHealth( 5 );
    }
    else if ( Input.GetKeyDown(Keycode.7) )
    {
        SubtractHealth( 7 );
    }

Now add this new function to the end of the script :

function SubtractHealth( theAmount : int )
{
	HEALTH -= theAmount;
	Debug.Log( "patay" + HEALTH );
}

when you play, hitting 5 or 7 reduces HEALTH by that amount, all just using one function and telling that function the Amount. cool huh!

How can you use this? Well imagine a light, medium and heavy spear. You could set up your collision event like this :

function OnCollisionEnter( theCollision : Collision )
{
    if( theCollision.gameObject.name == "LightSpear" )
    {
        SubtractHealth( 1 );
        Debug.Log( "Hit by a Light Spear !" );
    }
    else if( theCollision.gameObject.name == "MediumSpear" )
    {
        SubtractHealth( 3 );
        Debug.Log( "Hit by a Medium Spear !" );
    }
    else if( theCollision.gameObject.name == "HeavySpear" )
    {
        SubtractHealth( 5 );
        Debug.Log( "Hit by a Heavy Spear !" );
    }
}

That might be very handy later on. Hope this helps =]