Coroutine not stopping

Hey, thanks for dropping in. I have a problem where this coroutine just wont stop, and everytime i press the run button it stacks, so in game stamina is constantly dropping after you press shift once, and it stacks. The coroutine is rundrainstamina, and here’s the code:

    private void OnEnable()
    {
        runAction.performed += _ => InitialRun();
        runAction.canceled += _ => Walking();

        shootAction.performed += _ => ShootGun();
    }

    private void OnDisable()
    {
        runAction.performed -= _ => Walking();
        runAction.canceled -= _ => InitialRun();

        shootAction.performed -= _ => ShootGun();
    }
    private void Walking()
    {
        isRunning = false;
        currentSpeed = walkSpeed;
    }

    private void InitialRun()
    {
        if (currentStamina - initialRunCost >= 0 && isRunning == false)
        {
            UseStamina(initialRunCost);
            currentSpeed = runSpeed;
            isRunning = true;
            Running();
        }
    }

    private void Running()
    {
        if (currentStamina - runCost < 0 || !isRunning)
        {
            StopCoroutine(runStam);
            currentSpeed = walkSpeed;
        }

        runStam = StartCoroutine(RunDrainStamina());

    }

public void UseStamina(int staminaDrain)
    {
        if (currentStamina - staminaDrain >= 0)
        {
            currentStamina -= staminaDrain;
            staminaBar.SetStamina(currentStamina);

            if(regen != null)
            {
                StopCoroutine(regen);
            }
            regen = StartCoroutine(RegenStamina());
        }
        else
        {
            Debug.Log("Not enough stamina");
        }
    }

    private IEnumerator RegenStamina()
    {
        yield return new WaitForSeconds(staminaRegenStart);

        while(currentStamina < maxStamina)
        {
            currentStamina += maxStamina / 100;
            staminaBar.SetStamina(currentStamina);
            yield return staminaRegenTick;
        }

        regen = null;
    }

    private IEnumerator RunDrainStamina()
    {
        while (currentStamina - runCost >= 0)
        {
            UseStamina(runCost);
            yield return new WaitForSeconds(runDrainStaminaTick);
        }
    }

thanks to anyone who responds

shootAction.performed += _ => ShootGun();
Is causing you issues here. You’re creating a delegate from the ShootGun() method
You are not subscribing the ShootGun() method to this action, you are instead subscribing a newly created delegate that discards one parameter.

Instead, try saying
shootAction.performed += ShootGun;

Keep in mind that you’ll have to add the context argument to your ShootGun method so that the compiler lets you subscribe.

1 Like

Thanks for the reply! I did what you said and its working better now, but the only problem is it still drains stamina even when you stop running. If you know a fix for this, please enlighten me! Also, thanks again for helping out :slight_smile:

Edit: I got the solution! I just made a separate function for draining stamina while you’re running, and after some tweaking, I nearly have a fully functional stamina system! Thanks so much for your help Dextozz

1 Like