So I have made a health bar and it’s decreasing and shown in numbers up there using GUI and I have no problem with that, except that the bar itself -which is red in my case- isn’t scaling with the container. it’s far wide stretched! It’s only visible when I click play, it looks normal and scaled thou when I’m not playing the game. Any help please?
I have attached a picture showing this problem, my code is below.
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public float Health;
private SpriteRenderer Healthbar;
private Vector3 Healthscale;
private float HealthNumber = 0;
public float MaxHealth;
// Use this for initialization
void Start () {
Health = 100;
MaxHealth = 100;
Healthbar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
Healthscale = Healthbar.transform.localScale;
}
void HealthUpdate () {
Healthbar.transform.localScale = new Vector3 (Healthscale.x* Health *0.1f, 1, 1);
}
// Update is called once per frame
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Enemy"){
Health -= 25;
}
}
public void OnGUI () {
GUILayout.Label (HealthNumber.ToString ("Health" + Health));
}
void Update () {
if (Health <= 0) {
Application.LoadLevel (0);
}
HealthUpdate ();
if (Health > MaxHealth) {
Health = MaxHealth;
}
}
}
Theres a simpler way to do the healthbar.
Heres a vid about it: - YouTube