Using Coroutines to increase Float values C#

hey all, i have an issue with my code where i am using coroutines to make my stamina float go up and down depending on whether the left shift key is pushed, the stamina value goes down but refuses to go back up again. sorry for the mess i have had to butcher other codes to get this working since i cant really understand coroutines:

	float sprintSpeed = 5;
	float canSprint = 1;
	float originalMovementSpeed = 2;
	public float health = 1.0f;
	public float stamina = 1.0f;

void Update () {
        //Sprint
		if( Input.GetButton("Sprint")){
			if( canSprint == 1){
				if(stamina > 0.0f){
					StartCoroutine(staminaLoss(1, KeyCode.LeftShift));
					movementSpeed = sprintSpeed;
	}
			}
		}
		else{
			movementSpeed = originalMovementSpeed;
		}

		if( Input.GetButtonUp("Sprint")){
			if( stamina < 1){
				Debug.Log("staminup");
				StartCoroutine(staminaGain(1, KeyCode.LeftShift));
			}
		}
}


	public IEnumerator staminaLoss(float delay, KeyCode code){
		while(Input.GetKey(code)){
			stamina = stamina - 0.005f;
			yield return new WaitForSeconds(5);
			if(stamina < 0){
				stamina = 0;
			}
		}
	}

	public IEnumerator staminaGain(float delay, KeyCode code){
		while(Input.GetKey(code)){
			stamina = stamina + 0.005f;
			yield return new WaitForSeconds(5);
			if(stamina > 1){
				stamina = 1;
			}
		}
	}

Thanks for any help and i can post the full code if needed.

I would suggest throwing both the methods into a single coroutine. This makes more sense then starting and stopping both. It will simplify your Update as well. I believe the following code meets the intent of the original script, a little hard to tell what you were trying to do.

void Start (){
    StartCoroutine(StaminaControl(KeyCode.LeftShift));
}

public IEnumerator StaminaControl(KeyCode code){
    while (true){
        while(Input.GetKey(code) && stamina > 0){
            stamina = stamina - 0.005f;
            if(stamina < 0){
                stamina = 0;
            }
            movementSpeed = sprintSpeed;
            yield return null;
        }

        while(!Input.GetKey(code) && stamina < 1){
            stamina = stamina + 0.005f;
            movementSpeed = originalMovementSpeed;
            if(stamina > 1){
                stamina = 1;
            }
            yield return null;
        }
    yield return null;
    }
}

You have your stamina lossing corutine set up ok, but you probably copy-pasted for gaining. In the gain function you have “while(Input.GetKey(code))” and it is wired to run on ButtonUp. It won’t run, because Input.GetKey(code) will return false (since the user released the shift button, thus activating the ButtonUp).

Just replace the condition inside the gain while with something like “while(currentStamina < maxStamina)”.

sorry to answer my own question but a combination of Saad_Khawaja’s answer and adding the ! to:

if(!Input.GetButton("Sprint")){
    if( stamina < 1){
        StartCoroutine(staminaGain(1, KeyCode.LeftShift));
    }
}

seemed to work, thanks to all for the help