Search Unity

GUI for health

Discussion in 'Immediate Mode GUI (IMGUI)' started by john55223, Sep 25, 2013.

  1. john55223

    john55223

    Joined:
    Mar 4, 2013
    Posts:
    14
    I am trying to make a GUI to fit a health bar, and eventually other information. I want a red health bar to go over a black background, and the black background would have the health text on it....... meh

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HealthBar : MonoBehaviour {
    6.     public int maxHealth = 100;
    7.     public int curhealth = 100;
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.    
    19.     //GUI draw
    20.     void OnGUI(){
    21.         int width = Screen.width / 3;
    22.         int healthwidth = width / (maxHealth/curhealth);
    23.         GUILayout.
    24.         GUI.Box(new Rect(10,10,width, 20), "Health: " + curhealth + "/" + maxHealth);
    25.        
    26.         GUI.color = Color.red;
    27.         GUI.Box(new Rect(10,15,healthwidth, 10), " ");
    28.        
    29.     }
    30.    
    31. }
    32.  
    My issue is the fact that the bars are on the same layer? Therefore they are both black and useless? I would appreciate any assistance with this.
     
  2. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    No, it's just that Box skin is by default transparent, so you are seeing both boxes.

    You should map a custom GUISkin using the GUI.skin = _yourSkin just before drawing the box.