• 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
1
Question by eitanwass · Oct 20, 2016 at 02:30 PM · coroutineienumeratorstartstoprestart

Coroutine start and stop

Hello human beings!

I want to have a coroutine which moves an object but i also want to be able to stop the coroutine in the middle and start it from the beginning or restart it at any way...

What i have now:

 public IEnumerator walkCoroutine;
     void Start () {
             walkCoroutine = WalkingCoroutine ();
         StartCoroutine (walkCoroutine);
         }
     ...
     
    if (Input.GetKeyDown (KeyCode.H)) {
             StopCoroutine (walkCoroutine);
             print ("STOPED");
         }
         if(Input.GetKeyDown(KeyCode.J)){
             walkCoroutine = WalkingCoroutine ();
             StartCoroutine (walkCoroutine);
             print ("STARTED");
         }
     
 ...
     
    IEnumerator WalkingCoroutine(){
         iTween.MoveTo (gameObject, iTween.Hash (
             "path", walkingPath,
             "time", timeToWalk,
             "easetype", "linear",
             "looktarget", rotationIndicator,
             "looktime", 0.1f,
             "axis", "y", 
             "onupdate", "CheckIfOnNode", 
             "onComplete", "FinishPath"));
         
         yield return null;
     }

Please help me since it's very important for my project.

Ethan

Comment
Add comment
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

2 Replies

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

Answer by dhore · Oct 20, 2016 at 05:43 PM

Since you're already using iTween there's absolutely no need to be touching Coroutines. Just use iTween's Pause and Resume functions like so:

 void Start () {
     iTween.MoveTo (gameObject, iTween.Hash (
          "path", walkingPath,
          "time", timeToWalk,
          "easetype", "linear",
          "looktarget", rotationIndicator,
          "looktime", 0.1f,
          "axis", "y", 
          "onupdate", "CheckIfOnNode", 
          "onComplete", "FinishPath"));
 }
      ...
      
     if (Input.GetKeyDown (KeyCode.H)) {
              iTween.Pause(gameObject, "MoveTo");
              print ("STOPED");
          }
          if(Input.GetKeyDown(KeyCode.J)){
              iTween.Resume(gameObject, "MoveTo");
              print ("STARTED");
          }
  ...
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 eitanwass · Oct 20, 2016 at 06:17 PM 1
Share

Tx :) Really helped me

avatar image
0

Answer by Mikael-H · Oct 20, 2016 at 02:43 PM

Use the Couroutine reference that is returned by StartCoroutine:

EDIT: After your edit of the question I see your problem. The solution stopping a coroutine of course only works if there actually is a loop in it that performs work :)

 Coroutine walkingPath = null;
 
 void Start () {
         walkingPath  =  StartCoroutine ("WalkingPath");
      }
  ...
  
  if (Input.GetKeyDown (KeyCode.H) && walkingPath  != null) {
              StopCoroutine (walkingPath );
          }
          if(Input.GetKeyDown(KeyCode.J) && walkingPath  == null){
              walkingPath  = StartCoroutine ("WalkingPath");
          }
  
  ...
  
  IEnumerator WalkingPath(){

          while(true){
               //Some code to move
               yield return null;
           }
      }
 
Comment
Add comment · Show 3 · 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 eitanwass · Oct 20, 2016 at 02:52 PM 0
Share

It's not working....

 Coroutine walk = null;
 
 void Start () {
         walk = StartCoroutine ("WalkingPath");
     }
 
 //IN UPDATE:
 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.H) && walk != null) {
             StopCoroutine (walk);
             print ("STOPED");
         }
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.J) && walk == null){
             walk = StartCoroutine ("WalkingPath");
             print ("STARTED");
         }
 
 //NOT IN UPDATE:
 IEnumerator WalkingPath(){
         iTween.$$anonymous$$oveTo (gameObject, iTween.Hash (
             "path", walkingPath,
             "time", timeToWalk,
             "easetype", "linear",
             "looktarget", rotationIndicator,
             "looktime", 0.1f,
             "axis", "y", 
             "onupdate", "CheckIfOnNode", 
             "onComplete", "FinishPath"));
         
         yield return null;
     }
 

It does show the debug.log (print) but the movement doesn't stop.

avatar image Bunny83 · Oct 21, 2016 at 01:46 AM 1
Share

That won't change anything. Yes, using the Coroutine instance is easier and makes more sense, but using the IEnumerator instance works as well if you do it the way he did. The problem here wasn't stopping and restarting the coroutine since the coroutine finishes immediately. The iTween library implements it's own "coroutine-like" management. You only have to call "iTween.$$anonymous$$oveTo" once and it will start the tween.

avatar image eitanwass Bunny83 · Oct 21, 2016 at 06:05 AM 0
Share

Thanks for the clarification :)

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

63 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 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 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

Execution order when Start is declared with IEnumerator return (as a Coroutine) 1 Answer

Item duration doesn't stack 3 Answers

Update scene every 5 minutes 2 Answers

3rd person controller in c#? 1 Answer

making a 2D character Speak for different lenghts of time 2 Answers

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