Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

cant get health bar working

Discussion in 'Scripting' started by farbrorhumla, Dec 14, 2014.

  1. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
    no error showing :S and when i start game there is now health bar why?
    i draged script to first person controler


    using UnityEngine;
    using System.Collections;

    public class playerhealth : MonoBehaviour {
    public int maxHealth = 100;
    public int curHealth = 100;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    void OnGui() {
    GUI.Box (new Rect(10, 10, Screen.width / 2 / (maxHealth / curHealth), 20), curHealth + "/" + maxHealth);
    }
    }
     
  2. PixelScholar

    PixelScholar

    Joined:
    Nov 11, 2014
    Posts:
    4
    Try setting up a float variable that controls the length of your health bar. Something like this:

    Code (CSharp):
    1. float healthBarLength;
    Then in your OnGUI function write your statements like this:

    Code (CSharp):
    1. GUI.Box (newRect (10, 10, healthBarLength, 20), "");
    2.  
    3. healthBarLength = (Screen.width / 2) * ((float)curHealth / (float)maxHealth);
    Then try lowering your curHealth value in the inspector (you'll have to make it a public variable) and see if that scales your health bar down.

    Hope that helps.
     
    Rienhl likes this.
  3. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    Change:

    void OnGui()

    to:

    void OnGUI()

    It's a really simple mistake that can get easily overlooked ;)

    EDIT: PixelScholar's answer is a good thing to do too, it helps you avoid typos and stuff. But your problem is just the OnGui -> OnGUI thing.
     
    PixelScholar likes this.
  4. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
  5. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
    void OnGUI() funstion never show up on screen.
     
  6. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    I just copy/pasted your code and changed OnGui to OnGUI (check capitalization). What exactly isn't working?
     
  7. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    Are you using javascript or c#?
    Where are you applying the playerhealth script?

    EDIT: maybe you can't see it because of how your workspace is set up. GUI will appear only on Game view, not scene view. Also, try checking "Maximize on Play" (eventhough you wouldn't actually need it)

    I'm sorry if i tell you basic stuff but I don't know what's your level with Unity :p
     
    Last edited: Dec 14, 2014
  8. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
    c# i cant see the health bar on screen
     
  9. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
    i apply script on to first person controller
     
  10. farbrorhumla

    farbrorhumla

    Joined:
    Dec 14, 2014
    Posts:
    7
    OnGui to OnGUI omg thx alot =) i big fail for 4 hours, its my first day coding in unity 4
     
    Rienhl likes this.
  11. Rienhl

    Rienhl

    Joined:
    Nov 14, 2013
    Posts:
    42
    I guess you'll never forget that again hahaha