cant get health bar working

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);
}
}

Try setting up a float variable that controls the length of your health bar. Something like this:

float healthBarLength;

Then in your OnGUI function write your statements like this:

GUI.Box (newRect (10, 10, healthBarLength, 20), "");

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.

1 Like

Change:

void OnGui()

to:

void OnGUI()

It’s a really simple mistake that can get easily overlooked :wink:

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.

1 Like

cant get it work :frowning:

i follow this video to fix a health bar but its not working and its allways the same shit with coding cant get it work everytime i try and 0 error and still not working.

void OnGUI() funstion never show up on screen.

I just copy/pasted your code and changed OnGui to OnGUI (check capitalization). What exactly isn’t working?

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 :stuck_out_tongue:

c# i cant see the health bar on screen

i apply script on to first person controller

OnGui to OnGUI omg thx alot =) i big fail for 4 hours, its my first day coding in unity 4

1 Like

I guess you’ll never forget that again hahaha