- Home /
Problem with health bar
This is my health manager script, pls help me understand why the health bar dont shrink when health decrease`using UnityEngine; using System.Collections;
public class ThanatosFriend : MonoBehaviour {
public GameObject explosion;
public float ad;
public float hp;
public float maxhp;
public float regen;
private GUITexture health;
public GUIText regenText;
private Vector3 healthScale;
float healthRatio;
private Done_GameController gameController;
private ThanatosEnemy TEnemy;
private Done_PlayerController Player;
void Start()
{
health = GameObject.Find("Health").GetComponent<GUITexture>();
healthScale = health.transform.localScale;
regenText.text = "+"+regen;
InvokeRepeating("Regen",1,1);
GameObject TEnemyObject = GameObject.FindGameObjectWithTag("Enemy");
TEnemy = TEnemyObject.GetComponent<ThanatosEnemy>();
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
gameController = gameControllerObject.GetComponent <Done_GameController>();
UpdateHealthBar();
}
void Regen()
{
if (maxhp>hp)
{
hp+=regen;
}
}
void OnTriggerEnter(Collider other)
{
TEnemy.hp -=ad;
Instantiate(explosion, other.transform.position, other.transform.rotation);
Destroy (other.gameObject);
gameController.AddScore(TEnemy.scoreValue);
UpdateHealthBar();
if (other.tag == "EBolt")
{
hp -=TEnemy.ad;
Destroy(other.gameObject);
}
if (other.tag == "Boundary"||other.tag=="Bolt")
{
return;
}
}
public void UpdateHealthBar ()
{
healthRatio =1/hp;
health.color = Color.Lerp(Color.gray, Color.red, 1 - hp * healthRatio);
health.transform.localScale = new Vector3(0.1f,healthScale.y * hp*healthRatio, 1);
}
} `
Comment
Answer by YoungDeveloper · Jun 20, 2014 at 10:45 AM
Shouldn't updatehealthbar be in Update() or in OnTriggerEnter afer you remove the health?
I dont understand, can u tell me more detail(sry my English terrible)
Your answer
Follow this Question
Related Questions
Enemy Health Damage 2 Answers
Problem with Execution Order 0 Answers
health bar 2 Answers
How Do I Make A Health Bar 6 Answers
Health C# script problem. 3 Answers