Maximum of a bar

I have a bar with maximum 25.0 that decreases to 0.0. And if I press the key button “D”, the bar adds 10.0 If the bar is at 20.0 and I press the “D” key, it adds 10.0 so the bar have 30.0, but the maximum is 25.0

How can I fix this problem. If the bar have 20.0 of 25.0 and I press the “D” key that adds normally 10.0 it adds only 5.0 to fill the bar to 25.0 and not to 30.0,
5.0 above the maximum. Someone who can help me?

This is the script I use;

#pragma strict
 
var totalTime = 25.0;
var remainingTime = 25.0;
var BarBorder:Texture2D;
var BarBorderWidth:float=508;
var BarBorderHeight:float=28;
var Bar:Texture2D;
var BarWidth:float=500;
var BarHeight:float=20;
 
function Update() {
    if (remainingTime > 0.0)
       remainingTime -= Time.deltaTime;
    if (remainingTime < 0.0009){
           Application.LoadLevel("Sc_Intro_02");
       }
    if (Input.GetKeyDown("d")) {
      remainingTime += 10.0;
   }
}
 
function OnGUI(){
    GUI.DrawTexture(Rect( (Screen.width - BarBorderWidth)*0.5,(Screen.height - BarBorderHeight)*0.95, BarBorderWidth, BarBorderHeight), BarBorder);
    GUI.DrawTexture(Rect( (Screen.width - BarWidth)*0.5,(Screen.height - BarHeight)*0.945, BarWidth* remainingTime/totalTime, BarHeight), Bar);
}

Please help :smiley:

Pseudocode :

if ( currentValue > maximumValue )
{
    currentValue = maximumValue;
}

Check if your value has risen above the maximum value, if so then make the current value equal the maximum value.