Need support on creating a Health Bar

2D One.

Well, actually what i need is a “Health Bar” that scales by its X direction as WaitForSeconds keeps going.

In my game the character will enter in a area that makes him change the animation, and when he goes out of this area the “heath bar” will decrease untill 0px

I know i must use GUI.DrawTexture and ScaleMode, but he main problem is i don’t know to script it by code.

Don’t need to be perfect, i just need orientation or some part of the code.

Thanks in advance.

var myRect : Rect;
var myText : Texture;

function Start()
{
  myRect = Rect(*insert starting rect variable here*);
}

function Update()
{
  // Example way to change bar length
  myRect.width -= Time.deltaTime;
  if (myRect.width < 0) {
    myRect.width = 0;
  }
}

function OnGUI()
{
  GUI.DrawTexture(myRect, myText);
}

The above has all the concepts you need. The code in Update will shrink the bar from the right until it hits zero length, the OnGUI() code will draw it based on the changing Rect, and the Start() function will initialize its position.

Here’s another way. I think just a GUI box would work for a health bar, like this:

function OnGUI () {
   var health = 100;
   GUI.Box (Rect (10,10,health,10), "HEALTH");
};

and just change the ‘health’ variable as you need to.

Wow that works perfectly.

Thanks for everything. I will just try the code and scripting and if i get any problem i will return.

Cya [ ]'s