- Home /
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
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");
}
...
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;
}
}
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.
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.