coroutine not stop checking velocity

I have a problem with coroutine not stop with variable condition even i have set that bool variable.

i have ballcollision which start coroutine from start to check that the velocity is 0 or magnitude is lower than 0.1

using UnityEngine;
using System.Collections;
public class BallCollision3 : MonoBehaviour

{

public bool isFired;

public bool isPaused; 

    void Start ()
    {
        StartCoroutine(checkVector0());
    }

     private IEnumerator checkVector0()
     {

          while(true)
	     {

		if(isFired && !isPaused)//is fired from cannon and not be paused by button
	    {	 	   

	        if(rgdBdy.velocity == Vector3.zero || rgdBdy.velocity.magnitude < 0.1)//if zero
		    {
			   yield return _sync();//wait
			   if(rgdBdy.velocity == Vector3.zero || rgdBdy.velocity.magnitude < 0.1)//check again
				{							
                    gob.ToCenter();
					break;			
				}
		    }		 
		}
	   yield return null;	
	}
	Debug.Log("checkVector0 is Over");
}

////>>> yield return _sync();  put this at the end (or at any place you want to check for a pause) :

public Coroutine _sync(){ 
	return StartCoroutine(PauseRoutine());	
}



public IEnumerator PauseRoutine(){
	yield return new WaitForSeconds(5);
	yield return new WaitForEndOfFrame();	
}

public void set_isFired(bool mode)
{
	isFired = mode;
}

public void set_isPaused(bool mode)
{
	isPaused = mode;
}

public bool get_isPaused()
{
	return isPaused;
}

public bool get_isFired()
{
	return isFired;
}

}


Another class is PauseButton2 which set ballccollsion3 isPaused–>true when it is clicked in order to pause the game and then set ballcollision3’s velocity to zero and save original velocity. and if it is clicked again it will set isPaused–>false and redo velocity to its original

the problem is even i have set isPaused to true but checkVector0() is still check the velocity and bring the game over how can i solve this?

When execution flow falls into the _sync coroutine, there is nothing to prevent it from waiting exactly 5 seconds.

What you need to do is replaced WaitForSeconds(5) with something like:

public IEnumerator PauseRoutine(){
    float fTimeElapsed = 0.0f;
    while (fTimeElapsed < 5.0f && !isPaused) {
        fTimeElapsed += Time.deltaTime;
        yield return null;
    }
    yield return new WaitForEndOfFrame();
}

This solution will break the _sync coroutine when isPaused is true. If you want to pause the coroutine, then wait the remaining time when isPaused is false again, then do:

public IEnumerator PauseRoutine(){
    float fTimeElapsed = 0.0f;
    while (fTimeElapsed < 5.0f && !isPaused) {
        fTimeElapsed += Time.deltaTime;
        while (isPaused) {
            yield return null;
        }    
        yield return null;
    }
    yield return new WaitForEndOfFrame();
}