• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vice_Versa · Aug 02, 2015 at 01:06 AM · coroutineswaitend

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;
 
     }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Aug 02, 2015 at 08:01 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vice_Versa · Aug 03, 2015 at 07:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reading Instance Variables Inside a Coroutine 2 Answers

Does a coroutine end itself automatically? 2 Answers

How to make a function wait untill a specific bool is true? 2 Answers

Help with waiting for input? I'm too used to how Console.ReadLine() works. 0 Answers

How to properly use Yield for a coroutine? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges