How to make my health decrease?

Could someone please help me edit this code so my health decreases when it touches an object?

It’s just like a loading bar at the moment, and i have no clue how to fix it. I need this done today and i havent got a clue how to do this, so please help! :frowning:

var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;

function OnGUI()
{

    
    GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
        GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);


        GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
            GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
        GUI.EndGroup ();

    GUI.EndGroup ();

} 

function Update()
{
    barDisplay = Time.time * 0.05;
}

It looks like you have it working on a timer rather than a collision. Depending on how you’re doing collision detection, you would want that to trigger the bar’s “value” to change (what you’re currently using “barDisplay” for it seems) rather than the Update() function.

If what you’re really asking is “how do I do collision detection”, then you should do some research - there are tons of resources out there such as: Basic Collision Detection Tutorial