[Solved] Fix counting ?

private int sprint = 100;
public int sprintTime;
public bool sprintsBool;

void Update() {
if (Input.GetKey(KeyCode.I)) {
sprintTime --;
sprintsBool = true;
Debug.Log(" run ");
}
else {
sprintsBool = false;
}

if (sprintsBool == false) {
StartCoroutine("wait_sprit");
}

if (sprintTime >= sprint) {
sprintTime = sprint;
}
if (sprintTime <= 0) {
sprintTime = 0;
}
} // end update

IEnumerator wait_sprit() {
yield return new WaitForSeconds(2);
sprintTime ++;
}

my script is about counting up and down. when i press “I” button, it counts down. the problem is when i press “I” button it not counting down immediately !

Is the rest of the logic working, it just isn’t happening immediately enough?

When i press the button it should count down not delay.

Your wait_sprit coroutine is still being called for 2 seconds after the ‘I’ key is pressed, countering your subtraction. Either cancel your coroutine or choose a different method of tracking.

please give me one of those methods

as DQ said. I canceled my coroutine and used time, and it works :wink:

    private float sprint = 100;
    public float sprintTime;
    public bool sprintsBool;
    public int timer;

    void Update() {

        if (Input.GetKey(KeyCode.I)) {
            sprintTime --;
            sprintsBool = true;
            timer = 0;
            Debug.Log(" run ");
        }
        else {
            sprintsBool = false;
        }
        if (sprintsBool == false) {
            timer++;
            if (timer >= 50)     {
                sprintTime ++;
            }
   
        }
        if (sprintTime >= sprint) {
            sprintTime = sprint;
        }
        if (sprintTime <= 0) {
            sprintTime = 0;
        }

Thank you guys
[Solved]