Whew, I'm stuffed! (Hunger Script)

Back with my hunger script. Things are going good. Hunger increases every few seconds. If your hunger gets too high, you starve to death. So now I’ve included food into my game. And it works… a bit too well. Even taking hunger down to zero, it keeps going into negative #s, so conceivably a player could just keep eating and take their hunger down to negative infinity. I tried rectifying it in the playerHunger code:

static var curHunger : int = 0;
var maxHunger : int = 1000;
 
var hungertext : GUIText;
 
 
function Start ()
{
hungerRegen();
}
 
 
function Update ()
{
hungertext.text = "hunger " + curHunger + " / " + maxHunger;

 if(curHunger == 1000 )
Playerhealth.curHealth -= 1;

}
 

 
if(curHunger > maxHunger)
{
curHunger = maxHunger;
}

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

 
function hungerRegen ()
{
for(i=1;i>0;i++)
{
yield WaitForSeconds(10.0);
 
if(curHunger < maxHunger)
{
curHunger++;
}
}
}

Specifically the line that states

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

but that didn’t fix anything. Anyone kind enough to point out where I’m going wrong. I’m guessing that the code is right, it’s just in the wrong place. Thanks, and God bless.

Looks to me like everything between lines 24 - 32 needs to be inside your Update( ) function. i.e.

    function Update ()
    {
      hungertext.text = "hunger " + curHunger + " / " + maxHunger;
     
      if(curHunger == 1000 )
`    `{
        Playerhealth.curHealth -= 1;
      }
    
      if(curHunger > maxHunger)
      {
        curHunger = maxHunger;
      }
     
      if(curHunger <= 0 )
      {
        curHunger = 0;
      }
    }