Unable to get coroutines to stop

I have a selection of buttons and a 360 video will depend on which button you select. I want the user to have to have the raycast on the button for 5 seconds before the next video is playing.

This seems to be working okay, however I need the coroutine to stop when the ray is not on the button. I have tried to stop the coroutine when the ray is not on the correct menu item however it still continues. This is what I have tried so far:

public Coroutine coroutine;

void Update()
{

//create the ray to cast forward
RaycastHit hit;
Vector3 origin = transform.position;
Vector3 direction = transform.forward;
Ray ray = new Ray(origin, direction);
Debug.DrawRay(origin, direction * 100, Color.blue);

if (Physics.Raycast(ray, out hit))
{
objectCollided = hit.collider.gameObject.name;
hasHit = true;

if (objectCollided == “goForwardCube”)
{
coroutine = StartCoroutine(WaitAndPrint());
}
else if (objectCollided != “goForwardCube”)
{
StopCoroutine(coroutine);
}

}

IEnumerator WaitAndPrint()
{

// suspend execution for 5 seconds
ButtonControl.forwardCube.GetComponent().material.color = Color.clear;
forwardText.text = “5”;
yield return new WaitForSeconds(1);
ButtonControl.forwardCube.GetComponent().material.color = Color.blue;
forwardText.text = “4”;
yield return new WaitForSeconds(1);
ButtonControl.forwardCube.GetComponent().material.color = Color.clear;
forwardText.text = “3”;
yield return new WaitForSeconds(1);
ButtonControl.forwardCube.GetComponent().material.color = Color.blue;
forwardText.text = “2”;
yield return new WaitForSeconds(1);
ButtonControl.forwardCube.GetComponent().material.color = Color.clear;
forwardText.text = “1”;
yield return new WaitForSeconds(1);
videoPlayer.url = “Assets/Videos/1(360).mp4”;
ButtonControl.DisableButtons();
videoPlayer.Play();

}

Also, since implementing this, there seems to be a long pause before the next video plays and it seems quite laggy. Is there any way to improve this?

P.S. Posting here because everytime I try to ask a question on answers it takes me to my user account

You should use a Gaze Input Module and respond to the pointer enter events instead because if there is a raycast hit, it should call your coroutine every single frame during update and that’s probably causing most the problems.