- Home /
How do i stop a co-routine that is running from a different object but with the same script?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DynamicText : MonoBehaviour {
public string myString;
public Text Canvas;
private bool counter = true;
void OnTriggerEnter()
{
StopCoroutine("AutoType");
if (counter == true)
{
counter = false;
Canvas.text = null;
StartCoroutine("AutoType");
}
}
IEnumerator AutoType()
{
foreach (char letter in myString.ToCharArray())
{
Canvas.text += letter;
yield return new WaitForSeconds(0.1f);
}
yield return new WaitForSeconds(10);
Canvas.text = null;
}
}
By the way i can't use StopAllCoroutines because there are other coroutines that i need running from other scripts
You could use a static field to keep a reference of the active coroutine.
private static Coroutine mCoroutine; //shared between all instances
//...
void InitCoroutine()
{
if(mCoroutine != null)
StropCoroutine(mCoroutine);
mCoroutine = StartCoroutine(SomeCoroutine()); //each time you start a new coroutine assign it to the static field
}
or if you start it from your player script you could keep a reference of the Coroutine in there (not static).
Cheers!
Answer by mani3307 · May 21, 2018 at 09:09 AM
@Fred1998 I think you can refer that particular script by a "public scriptname other",drag the gameobject the script is attached to in the inspector and use other.stopcoroutine("coroutinename") in the current script
That is a good solution. but i have multiple gameobjects with that script, It would work in this instance but if i had something like 50 gameobjects with that script i would have to attack each and every one of them to each other, which would take forever.
If you had multiple gameobjects you can use tags $$anonymous$$aybe If you explain the scenario I can elaborate more on this
Yeah, so basically i have various gameobjects with colliders, if the player collides with them the co-routine starts, but when the player then goes through the next gameobject that possesses the same script the co-routine that is running already doesnt stop which overlaps with the one that is starting since the player collided with a new object. The script assigned to multiple game object is also referencing a gameobject with a canvas that displays text. So the end result is that as the player touches the new object the text goes weird since both co-routines are trying to type the text on the same canvas.
Answer by Deeblock · May 21, 2018 at 09:03 AM
The coroutine you reference in StopCouroutine must be the exact same reference as the one in StartCoroutine. Hence, you will need a variable to hold the coroutine in.
private IEnumerator myCoroutine;
void Start() {
if (myCoroutine != null) {
StopCoroutine(myCoroutine);
myCoroutine = null;
}
myCoroutine = MyCoroutine();
StartCoroutine(myCoroutine);
}
private IEnumerator MyCoroutine() {
for (int i = 0; i < 10; i++) {
print ("blah");
yield return null;
}
myCouroutine = null; // Reset your coroutine variable once coroutine is finished
}
It doesn't work because the stopCoroutine(myCoroutine) is still referencing itself, while it should be referencing the same script but on another game object. To put it more simply, i have script A attached to both game object 1 and 2. and i need the co-routine of script A that is attached to object 1 (which is running currently) to stop and let script A attached to game object 2 to start.
As far as I can see, you shouldn't be using coroutines writing to the same canvas on multiple objects. If that canvas takes in input from multiple gameobjects, consider making a script that references the canvas, and put only one coroutine on the canvas itself. This way, whatever object that calls the coroutine will stop the only coroutine and runs its own function.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Coroutine works in editor, but not in build Windows application 0 Answers
Terminal-like GUI, wait for input 1 Answer
Coroutine gets stuck randomly? 1 Answer
Startcoroutine not working second time 2 Answers
Issues with my loading screen 0 Answers