How do I set a max limit for my health value?

In the game I’m working on, the Main Character can pick up food to recover health (each piece recovers 10 points).
I want the max value to be 100, but if I keep picking up food the script adds 10 points to the current value, and I end up having 110, 120, 130 points, etc.
I want a script to limit the value so it can’t be higher than 100 points.

I’ve been working on it but I can’t seem to find a simple solution; is there an easy way to set a max limit so the points won’t be higher than 100?

Thanks.

Var health : int = 100;

Function update ()
{
   If(health > 100)
   {
      health = 100;
   }
}

I’m pretty new to Unity myself but I think this may be able to be solved with a simple if statement. For example:

if(healthPoints >= 100){

healthPoints = 100;

}

You could put this in the Update() function so that it checks on every frame. If I’m right, this should prevent healthPoints from exceeding 100 by setting the value to 100 every time it exceeds that number. Oh, and this is in Javascript :slight_smile:

or you can check your life only when you pick up the food
c# :

void PickUpFood () 
{
   health = health + 30;

   if(health > 100)
   {
      health = 100;
   }
}

You can make it shorter since it’s only one line under the if statement.

var health = 100;

function Update ()
{
   if(health > 100)
      health = 100;
}