Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

3rd person controller in c#? 1 Answer

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

Update scene every 5 minutes 2 Answers

Timer that stops at the end of the game 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges