- Home /
Is there a way to end a Coroutine Early?
So heres what im trying to do, i have a cursor set up, that animates when it collides with certain objects. the cursor plays an animation (to show that your selecting the object) for two seconds. after two seconds that object will be selected. If in that two seconds, the player decides they dont want to select that object, and they move the curso off, the animation should stop playing, and the object should not be selected. I have the animation set up in a Coroutine which is called in an OnTriggerEnter Function, then i have a StopCoroutine call in the OnTriggerExit function. when the player moves the cursor away, the animation stops playing and the curosr goes back to normal. The problem i am having is that after two seconds, the object still gets selected. Any ideas how i can fix this? heres my code
public bool enumBool = false;
private bool waitBool = true;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name.Contains ("Arrow")) {
Orbit.isMoving = false;
StartCoroutine(WAIT());
if (col.gameObject.name.Contains ("Up")) {
col.gameObject.transform.GetChild (0).transform.GetComponent<ChangeLabelColor> ().changeColor ();
if (enumBool == true){
StartCoroutine(moveThumbnailsUp());
}
}
if (col.gameObject.name.Contains ("Down")) {
col.gameObject.transform.GetChild (0).transform.GetComponent<ChangeLabelColorDOWN> ().changeColor ();
if (enumBool == true)
{
StartCoroutine(moveThumbnailsDown());
}
}
}
void OnTriggerExit(Collider col)
{
enumBool = false;
StopCoroutine (WAIT());
}
IEnumerator WAIT(){
waitBool = true;
transform.parent.GetComponent<changeCursor>().hideCursor();
transform.parent.GetComponent<changeCursor>().playAnimation();
enumBool = true;
yield return new WaitForSeconds(2);
transform.parent.GetComponent<changeCursor> ().goBackNormal ();
waitBool = false;
}
IEnumerator moveThumbnailsDown()
{
while (waitBool)
yield return new WaitForSeconds (0.1f);
Debug.Log ("MOVING DOWN");
transform.parent.GetComponent<changeCursor>().goBackNormal();
thumbnail1.GetComponent<Orbit> ().canMoveUp = true;
thumbnail2.GetComponent<Orbit> ().canMoveUp = true;
thumbnail3.GetComponent<Orbit> ().canMoveUp = true;
thumbnail4.GetComponent<Orbit> ().canMoveUp = true;
thumbnail5.GetComponent<Orbit> ().canMoveUp = true;
thumbnail6.GetComponent<Orbit> ().canMoveUp = true;
thumbnail7.GetComponent<Orbit> ().canMoveUp = true;
thumbnail8.GetComponent<Orbit> ().canMoveUp = true;
thumbnail9.GetComponent<Orbit> ().canMoveUp = true;
thumbnail10.GetComponent<Orbit> ().canMoveUp = true;
thumbnail11.GetComponent<Orbit> ().canMoveUp = true;
thumbnail12.GetComponent<Orbit> ().canMoveUp = true;
thumbnail13.GetComponent<Orbit> ().canMoveUp = true;
thumbnail14.GetComponent<Orbit> ().canMoveUp = true;
thumbnail15.GetComponent<Orbit> ().canMoveUp = true;
thumbnail16.GetComponent<Orbit> ().canMoveUp = true;
thumbnail17.GetComponent<Orbit> ().canMoveUp = true;
thumbnail18.GetComponent<Orbit> ().canMoveUp = true;
thumbnail19.GetComponent<Orbit> ().canMoveUp = true;
thumbnail20.GetComponent<Orbit> ().canMoveUp = true;
thumbnail21.GetComponent<Orbit> ().canMoveUp = true;
thumbnail22.GetComponent<Orbit> ().canMoveUp = true;
thumbnail23.GetComponent<Orbit> ().canMoveUp = true;
thumbnail24.GetComponent<Orbit> ().canMoveUp = true;
enumBool = false;
}
IEnumerator moveThumbnailsUp()
{
while (waitBool)
yield return new WaitForSeconds (0.1f);
Debug.Log ("MOVINGUP");
transform.parent.GetComponent<changeCursor>().goBackNormal();
thumbnail1.GetComponent<Orbit> ().canMoveDown = true;
thumbnail2.GetComponent<Orbit> ().canMoveDown = true;
thumbnail3.GetComponent<Orbit> ().canMoveDown = true;
thumbnail4.GetComponent<Orbit> ().canMoveDown = true;
thumbnail5.GetComponent<Orbit> ().canMoveDown = true;
thumbnail6.GetComponent<Orbit> ().canMoveDown = true;
thumbnail7.GetComponent<Orbit> ().canMoveDown = true;
thumbnail8.GetComponent<Orbit> ().canMoveDown = true;
thumbnail9.GetComponent<Orbit> ().canMoveDown = true;
thumbnail10.GetComponent<Orbit> ().canMoveDown = true;
thumbnail11.GetComponent<Orbit> ().canMoveDown = true;
thumbnail12.GetComponent<Orbit> ().canMoveDown = true;
thumbnail13.GetComponent<Orbit> ().canMoveDown = true;
thumbnail14.GetComponent<Orbit> ().canMoveDown = true;
thumbnail15.GetComponent<Orbit> ().canMoveDown = true;
thumbnail16.GetComponent<Orbit> ().canMoveDown = true;
thumbnail17.GetComponent<Orbit> ().canMoveDown = true;
thumbnail18.GetComponent<Orbit> ().canMoveDown = true;
thumbnail19.GetComponent<Orbit> ().canMoveDown = true;
thumbnail20.GetComponent<Orbit> ().canMoveDown = true;
thumbnail21.GetComponent<Orbit> ().canMoveDown = true;
thumbnail22.GetComponent<Orbit> ().canMoveDown = true;
thumbnail23.GetComponent<Orbit> ().canMoveDown = true;
thumbnail24.GetComponent<Orbit> ().canMoveDown = true;
enumBool = false;
}
You can end a Coroutine from within with
yield break;
From the outside, you have to start it with the alternate way of passing the name of the Coroutine as a string to the StartCoroutine function:
StartCoroutine("YourCoroutineName");
Then you can use StopCoroutine("YourCoroutineName");
or
StopAllCoroutines();
To stop it.
Note that StartCoroutine(string) also accepts one object value as an optional parameter that is passed to the Coroutine as a parameter.
Answer by GiyomuGames · Aug 02, 2015 at 01:30 AM
What you can do is have a bool variable called "SelectAfter2Seconds" (you can chose your own name! ^^)
In OnTriggerEnter, if (col.gameObject.name.Contains ("Arrow")) you can do SelectAfter2Seconds = true; before or after starting your coroutine. Then in OnTriggerExit you do SelectAfter2Seconds = false; Finally in your coroutine Wait, after your line yield return new WaitForSeconds(2); you can check if SelectAfter2Seconds is still true or if it is false. If it is false then you don't execute the remaining code.
Im marking this as answered because the code is functioning way better than before. thank you good sir. the new issue im having is if i select one arrow, then quickly select another one, all of the colliders become disabled. Though, i think this is a result from a different bug in how my code is organized
Your answer