Stopping A Value From Being Less Than Zero

PROBLEM SOLVED

I had a brain blast shortly after writing this post, but it may be of use to someone someday. All i had to do was move the section of the script that reads:

if (energyCur< 0){
   energyCur= 0;
   }

down into when the shift key is pressed

I also added in a feature to stop the bar from going below zero as well.

if (scale < 0){
scale =0;
}

Here is the full code for reference:

UPDATED CODE

var scale = 1.0f;
public var energyStart: float;
public var energyCur: float;
public var energyMax= 100;
public var energyCostPerSecond: float= 1;

   function changeEnergy(Change:float){
   energyCur += Change;
   if (energyCur> energyMax){
   energyCur = 100;}
}  

function OnGUI() {
    GUI.backgroundColor = Color.yellow;
    GUI.Button(Rect(10, 40, 100 * scale, 20), "Energy");
   scale = energyCur / energyMax; 
if (Input.GetKey(KeyCode.LeftShift))
{
    energyCur -= energyCostPerSecond * Time.deltaTime;
if (energyCur< 0){
   energyCur= 0;
   }
if (scale < 0){
scale =0;
}
}
}

     function OnControllerColliderHit(hit : ControllerColliderHit) {
        if (hit.gameObject.tag == "Player")
            return;
        Debug.Log("You hit " + hit.gameObject.tag);
        if(hit.gameObject.tag == "ground")
        {
            //Destroy(hit.gameObject);
            changeEnergy (1);
        }

/*

Hello again,

Im having a minor issue that is really bugging me. I have a javascript file for a regenerating bar, it works fine, and i managed to get it working, but when the bar reaches zero it continues depleting, going into negative values.

the section of my script that has the code:

function changeEnergy(Change:float){
   energyCur += Change;
   if (energyCur> energyMax){
   energyCur = 100;}
   
if (energyCur< 0){
   energyCur= 0;
   }
}

and the full script for reference.

var scale = 1.0f;
public var energyStart: float;
public var energyCur: float;
public var energyMax= 100;
public var energyCostPerSecond: float= 1;

   function changeEnergy(Change:float){
   energyCur += Change;
   if (energyCur> energyMax){
   energyCur = 100;}
   
if (energyCur< 0){
   energyCur= 0;
   }
}  


function OnGUI() {
    GUI.backgroundColor = Color.yellow;
    GUI.Button(Rect(10, 40, 100 * scale, 20), "Energy");
     
   scale = energyCur / energyMax; 
if (Input.GetKey(KeyCode.LeftShift))
{
    energyCur -= energyCostPerSecond * Time.deltaTime;
}
}
    function OnControllerColliderHit(hit : ControllerColliderHit) {
        if (hit.gameObject.tag == "Player")
            return;
        Debug.Log("You hit " + hit.gameObject.tag);
        if(hit.gameObject.tag == "ground")
        { ;
            changeEnergy (1);
        }
    }

*/

Use Mathf.Clamp, like Mathf.Clamp(energyCur,0,energyMax);