- Home /
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.
set up a timer and when timer is greater than setTime int- x amount then reset timer on a click int equals x amount
You should select an answer so this question doesn't remain open, or add comments back if it's still unclear.
Answer by theAfrican · Jun 14, 2013 at 04:37 AM
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
someone gave you a thumbs down, I don't know why, thumbs down should only be for incorrect statements, so I gave it a thumbs up. it's essentially fine except it's not called every 60 seconds like he wanted, and that's trivial.
Answer by SinisterRainbow · Jun 14, 2013 at 04:50 AM
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
Answer by umangindianic · Jun 14, 2013 at 08:53 AM
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.
Your answer
Follow this Question
Related Questions
Why can't won't my if then statements work? 1 Answer
Detecting multiples of 2 3 Answers
Decrease int variable over time 4 Answers
Playerprefs across Scenes 1 Answer