Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

gui.box not showing need some help

Discussion in 'Scripting' started by william-ali, Dec 4, 2012.

  1. william-ali

    william-ali

    Joined:
    Dec 4, 2012
    Posts:
    7
    Hello there i made an hp bar in unity 3d with gui.box But after i wrote the code and fixed all errors, i cant get it to show in the game can anyone please assist me with that.

    here is the code :

    using UnityEngine;
    using System.Collections;

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


    public float healthBarLenght;


    // Use this for initialization
    void Start () {
    healthBarLenght = Screen.width / 2;


    }

    // Update is called once per frame
    void Update () {
    AddJustCurrentHealth (0);

    }

    void onGUI() {
    GUI.Box(new Rect(10, 10, healthBarLenght, 20), curHealth + "/" + maxHealth);



    }

    public void AddJustCurrentHealth(int adj){
    curHealth += adj;

    if (curHealth < 0)
    curHealth = 0;

    if (curHealth > maxHealth)
    curHealth = maxHealth;

    if (maxHealth < 1)
    maxHealth = 1;



    healthBarLenght = (Screen.width/2) * (curHealth / (float)maxHealth);

    }


    }


    thx for all the help i can get here!
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    I would rename your GUI method from "onGUI" to "OnGUI" and see if that gets you there, also of course that the script it attached to an object in the scene, but try the case change on the method.

    As a note, please format your code with code tags, an explanation can be found here.
     
  3. william-ali

    william-ali

    Joined:
    Dec 4, 2012
    Posts:
    7
    oh thanks alot! dident even think of that even tho i read it true like 100 times, and for the code tags i will do that if i post again, thx again!