- Home /
How to stop a coroutine with multiple parameters?
I know about StopAllCoroutines() that stops all coroutines and yield break that stops it from within the coroutine; But Is there a way how to stop one Coroutine with multiple parameters from outside?
Thanks
StopCoroutine
takes a IEnumerator
parameter since 4.5 so you should be able to pass in any coroutine with arbitrary number of arguments, have you tried that?
Thanks, I realised that I still have 4.3, I'll update it and try it out
Answer by hypnoticmeteor · Jan 10, 2015 at 03:09 PM
using UnityEngine;
using System.Collections;
public class testCo : MonoBehaviour {
IEnumerator play(int one, int two, int three)
{
print(one);
print(two);
print(three);
yield return null;
}
void Update () {
int t = (int)Time.time;
if (t < 10)
{
StartCoroutine(play(1, 2, 3));
}
else {
StopCoroutine("play");
print("stopped");
}
}
}
I do not know about the guy who was told me I was wrong. To stop a coroutine just
StopCoroutine("functionName").
To call it from outside this script put the line in a public function and call it.
Do you have any idea, why this doesn't work?
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour
{
IEnumerator play (int one, int two, int three)
{
while (true) {
print (one);
print (two);
print (three);
yield return null;
}
}
public void On$$anonymous$$ouseUp ()
{
print ("stopped");
StopCoroutine ("play");
return;
}
public void On$$anonymous$$ouseDown ()
{
print ("started");
StartCoroutine (play (1, 2, 3));
return;
}
}
(it's attached to a gameObject ofc)
using UnityEngine;
using System.Collections;
public class testCo : $$anonymous$$onoBehaviour {
bool set;
IEnumerator play(int one, int two, int three)
{
print(one);
print(two);
print(three);
yield return null;
}
void Update () {
if (set)
{
StartCoroutine(play(1, 2, 3));
}
else {
StopCoroutine("play");
print("stopped");
}
}
public void On$$anonymous$$ouseUp()
{
set = false;
}
public void On$$anonymous$$ouseDown()
{
set = true;
}
}
Your answer
Follow this Question
Related Questions
NullReferenceException in StartCoroutine 1 Answer
Flash image while ammo is low - coroutines? 3 Answers
Coroutines not passing yield 1 Answer
Collection Change During Iteration 0 Answers
Why doesnt my coroutine ever end? 2 Answers