Screen.width ~ HealthBar problem - C#

I’ve spent over an hour setting up the GUI.Box in C# for a “healthbar” and even though the code compiles, it seems the logic of the script seems to be correct, the “healthbar” wont show up when playtested and the float value healthBarLength shows up as 0 inside the unity editor. I wanted it to work the following way: GUI.Box is 1/2 of the screenwidth, it becomes shorter when you lose health… It shows nothing when you drag. Also, where should I place the Float healthBarLength for the script to work, if I put it just next to

public int curHealth = 100;
public float healthBarLength = (curHealth / maxHealth);

It wont compile at all ;/

using UnityEngine;

using System.Collections;

public class healthbar : MonoBehaviour {

public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;

// Use this for initialization
void Start () {
	OnGui();
}

// Update is called once per frame
void Update () {
}
void OnGui()
{
	healthBarLength = ((Screen.width / 2) / (curHealth / maxHealth));
	GUI.Box(new Rect(100, 100, healthBarLength, 20), curHealth + "/" + maxHealth);
		
}
}

You are doing two mistakes here.
First, rename the method from

void OnGui() {}

to

void OnGUI () {}

Method name is case-sensitive.

Second, you should not call OnGUI() from within Start() function.
OnGUI() will be called automatically/repeatedly by unity.

using UnityEngine;
using System.Collections;

public class PlayerGUI : MonoBehaviour {

public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;

// Use this for initialization
void Start () {
//OnGUI();
}

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

}

void OnGUI()
{
healthBarLength = ((Screen.width / 2) / (curHealth / maxHealth));
GUI.Box(new Rect(100, 100, healthBarLength, 20), curHealth + “/” + maxHealth);

}
}

I ran your code seems to work fine on mine but just to let you know you don’t need to have OnGUI in the Start function as OnGUI will update automatically