C# - Coroutines for Abilities

Hi. I am currently trying to create an Ability system in unity using coroutines. I already have the controls set up to activate them, but coroutines always seem a bit tricky to use properly. Simply, each ability the player has will use a cooldown of a set amount and count down until 0. The ability will be unusable during the cooldown, and when the cooldown reaches 0 the ability can be used again, just like in many RPG and Action games.

One Ability for example (below) uses a coroutine to set the players max speed to a higher value, and then reset it after the cooldown reaches 0. However, the coroutine keeps repeating, as it is not stopped anywhere except after the cooldown, and the maxspeed is always increasing, instead of staying at the fixed number.

IEnumerator Dash(){
		while (true) {
			Debug.Log ("Dash used.");

			float maxspeed = PlayerMaxSpeed *= 1.25f;
			float minspeed = PlayerMaxSpeed;

			PlayerMaxSpeed = maxspeed;

			yield return new WaitForSeconds (//whatever the cooldown is, say 10 seconds);

			PlayerMaxSpeed = minspeed;

			yield return false;
		}
	}

Any help in this issue will be greatly appreciated.

The while loop is calling this co routine logic again every frame. Simply remove while loop and it will be all fine.

 IEnumerator Dash(){
             Debug.Log ("Dash used.");
 
             float maxspeed = PlayerMaxSpeed *= 1.25f;
             float minspeed = PlayerMaxSpeed;
 
             PlayerMaxSpeed = maxspeed;
 
             yield return new WaitForSeconds (//whatever the cooldown is, say 10 seconds);
 
             PlayerMaxSpeed = minspeed;
 
             yield return null;
         
     }