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;
}
}
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
}
@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