How do i restart a Coroutine?

I’m trying to make a combo chain in my script. It goes like this.

I’m sorry if this is a little confusing so I made some indications of my code, down below it.

	public void Combos(){
		// COMBO CHAIN 1
		if (Input.GetKey (KeyCode.X) && Restart == true && Continue == false && Combo == 0) {
			CTime = 2f;
			Combo = 1;
			ReTime = 3f;
			Continue = false;
						anim.SetTrigger ("AttackSoft");
						Attacking = true;//My Attacking Boolean
						StartCoroutine (ACoroutine ());
						Cloned = (GameObject)Instantiate (HitBox, transform.position, transform.rotation);
			StartCoroutine (CTimer ());
			StartCoroutine (Destroy());
			StartCoroutine (ComboRestart());
		}

		// COMBO CHAIN 2
				if (Input.GetKey (KeyCode.X) && Continue == true && Combo == 1 && Restart == false) {
			Continue = false;
			ReTime = 4f;
			Combo = 2;
			print ("Combo Chain 2");
			CTime = 3f;
			StopCoroutine (CTimer ());
			StopCoroutine (ComboRestart());
			Coroutine
			StartCoroutine (CTimer ());
			StartCoroutine (ComboRestart());

				}

		// COMBO CHAIN 3
				if (Input.GetKey (KeyCode.X) && Continue == true && Combo == 2 && Restart == false) {
						print ("Combo Chain 3 You ended the Combo");

						Combo = 3;
			StartCoroutine (ComboRestart());
		}

CTime
This is my duration time Coroutine which tells me if my character can Continue attacking again or not.

Here’s the Code for it.

	IEnumerator CTimer(){
		Continue = false;
		print ("Hold On!");
		yield return new WaitForSeconds (CTime);
		print ("You may attack again");
		Continue = true;

	}

ReTime

This is my Duration Time for the Coroutine which tells me if my Characters Combo Chain Restarted or not.

Here’s the Code

	IEnumerator ComboRestart(){
		Restart = false;
		yield return new WaitForSeconds (ReTime);
		print ("Combo restarted");
		Restart = true;
		
	}

Combo

This is just a number that signifies which Combo number I’m currently at.

The reason why my Timer’s numbers were increasing is because I thought by increasing the number of my timer it will increase the duration, but it didn’t.

What my problem here is. Everytime I press the X Key Again for the second time.
Continue Switches to False, and Restart is Checked as True.

Like everytime!

I’d appreciate anyone who could help me with this.

There are no else ifs and your coroutines don’t wait before setting Continue and Restart.

Continue is being set and reset before ever reaching the if statement to check if it’s combo 2 or combo 3 so your checks are irrelevent.

I’m having trouble understanding it probably because I’m in a hurry but based on my quick look at your stuff I am guess it’s because you’re starting multiple coroutines. You can have multiple coroutines of the same name start at any given time.

So…
If you start coroutine(A) and it sets bool x to false and waits for 1 second before setting bool x to true maybe it does some more waiting for other things

and after it has done its waiting you accidentally start another coroutine(A) then bool x will go back to false even though the first instance of coroutine set it to true AND is still running.

Check this awesome script made by another user: TaskManager. Is a wrapper to call coroutines that allows you to pause them or stop them whenever you want. If you want to restart, you can stop the one you are currently using and start one anew.