How do I subtract a variable multiple times.

I’m trying to make a timer for a game that I’m working on, but the my variable is only being subtracted once while being in the Update() function. I have to take user input to make the timer. My code is

public void ReadScriptInput(string Time)
{
 
    input = Time;
    Debug.Log(input);
    active = true;


}

void Update()
{
         if ( result >= 0) {
        
            result = double.Parse(input) - subTime;
            result = result - subTime;
            Debug.Log(result + "   result");
               
  }

}  

}

This only subtracts my variable twice.

Hey there,

your current code says:

var result = originalTime;
result = result - subTime;
result = result - subTime;

which is the same as

result = originalTime - 2*subTime;

so as we can see result will always be the same, as it always is reset to a time which is relative to the original one.

So what you should do is:

public void ReadScriptInput(string Time)
{
     //directly set the time here so that it only is "initialized" once:
     result = float.Parse(Time);
     Debug.Log($"starting with time: {result}");
     active = true;
}

 void Update()
 {
        if ( result >= 0) {
             //only do substraction here, do *not* use the original value:
             result = result - subTime;
             Debug.Log($"remaining Time: {result}");
         }
 }