Subtract from Integer

Hello. I have tried for quite some time to get this working, but I can’t. How can I subtract from an integer every 60 seconds in case a gameobject is right clicked? If said object is right clicked, it refills back to 100. All help is appreciated.

function Start()
{
StartCoroutine(“DoEvent”);
}

    function DoEvent()    
    {    
          yield WaitForSeconds(1);
// do integer subtraction here
          StartCoroutine("DoEvent");
    }

for the right click you can handle that in an onmouse event

I created a separate class with static functions for timers, and I recommend you do so as well. Using callbacks with startcoroutine I find is the best way. Make sure it inherits from monobehaviour). This example below subtracts y from x for 60 seconds. If you are careful, you can call the _SetTimer(…) again inside the subtract function so it’s called everytime.

public class YourClass : monobehaviour {

private int x;
private int y;

private IEnumerator _SetNewTimer( float fDelay, System.Action callBack ) {
		yield return new WaitForSeconds( fDelay );
		if ( callBack != null )
			callBack();
	}


private void _DoSubtract() {
   x -= y;
}
...
private void SomeFunction() {
  ...
  StartCoroutine(_SetNewTimer(60.0f,_DoSubtract));
  ...
}
...
} //end class

First create Timer for 60 seconds and check the if timer is equal to 60 seconds subtract the value at the same time and if it is right clicked that refill the value with 100. same thing you can update n number of times.