Help me fix my health bar script

**When the current health bar (GUI.Box) equal 0 it’s outside the health bar, please help me to fix it into the health bar please!!

15093-untitled-1.jpg**

    using UnityEngine;
    using System.Collections;
     
    public class Playerhealth : MonoBehaviour {
        //public GUISkin MetalGUISkin;
		public int maxHealth = 200;
        public int curHealth = 100;
		public int maxExp = 200;
        public int curExp = 100;
		public int LV = 5;
		public static bool gender = false;
		public string Status = "Normal";
		
        public float healthBarLength;
		public float healthBar;
		public float expBarLength;
        // Use this for initialization
    void Start () {
        healthBarLength = 300;
		
        }
       
        // Update is called once per frame
    void Update () {
        AdjustCurrentHealth(0);   
        }
        
       
    void OnGUI(){
		//GUI.skin = MetalGUISkin;
		if (gender == true){
			GUI.Label(new Rect(80,10,healthBar, 20), "Gender: Male");
		}
		else 
			GUI.Label(new Rect(80,10,healthBar, 20), "Gender: Female");
		GUI.Label(new Rect(10,10, healthBar, 20), "LV: " + LV); 
		GUI.Label(new Rect(10,40, healthBar, 20), "HP: "); 
		GUI.Label(new Rect(200,10, healthBar, 20), "Status: " + Status); 
		GUI.Box(new Rect(40, 40, healthBar, 20), curHealth+ "/" + maxHealth);
		GUI.Box(new Rect(43, 42, healthBarLength - 5, 15), "");
		GUI.Label(new Rect(10,80, healthBar, 20), "Exp: "); 
		GUI.Box(new Rect(40, 80, healthBar, 20), curExp + "/" + maxExp);
		GUI.Box(new Rect(43, 82, expBarLength - 5, 15), "");
		}
    public void AdjustCurrentHealth(int adj) {
            curHealth += adj;
           
            if(curExp < 0)
         	curExp = 0;
            
            if(curExp > maxExp)
                curExp = maxExp;
		
            if(maxExp < 1)
                maxExp = 1;
		
			if(curHealth < 0)
         	curHealth = 0;
            
            if(curHealth > maxHealth)
                curHealth = maxHealth;
		
            if(maxHealth < 1)
                maxHealth = 1;
           
            healthBarLength = 300 * (curHealth / (float)maxHealth);
			healthBar = 300;
			expBarLength = 300 * (curExp / (float)maxExp);
        }
    }

Your issue is here, I think:

GUI.Box(new Rect(43, 42, healthBarLength - 5, 15), "");

You’re drawing the bar with a width equal to the calculated health bar length, minus 5. So what happens when the health bar length is 0? Well, obviously, it tries to draw a box with a width of -5, which projects it to the left instead of the right, causing the effect you’re seeing. What you really need to do is factor in the difference of 5 here:

            healthBarLength = 300 * (curHealth / (float)maxHealth);

Which should ensure the length never drops below 0.

  1. GUI.Box(new Rect(43, 40, healthBar, 20), curHealth+ “/” + maxHealth);
  2. GUI.Box(new Rect(40, 42, healthBarLength - 5, 15), “”);

if it didn’t work just comment and i will test and provide fix as soon as i get to my PC