Starting and stopping Coroutines

Hi all,

I’m trying to make a subtitle system where:

  1. text is displayed
  2. for an amount of time
  3. after which, it disappears
  4. OR the player can click to make it disappear
  5. There can be a chain of text

The class for the text and time:

class SubtitleThread (MonoBehaviour):
	public text as string
	public timer as single = 5.0

The class to manage it:

class SubtitleSystem (MonoBehaviour): 
	public static showSubtitle as bool = false
	public static subtitleText as string = ""

	def PlaySubtitle (subtitleThreads as (SubtitleThread)) as System.Collections.IEnumerator:
		for eachThread in subtitleThreads:
			yield StartCoroutine("PlaySubtitleCR", eachThread)
			showSubtitle = false
			subtitleText = ""

	def PlaySubtitleCR (oneThread as SubtitleThread) as System.Collections.IEnumerator:
		subtitleText = oneThread.text
		showSubtitle = true
		yield WaitForSeconds(oneThread.timer)

	def ClearSubtitle ():
		StopCoroutine("PlaySubtitleCR")

Clicking while showSubtitle == true will call ClearSubtitle().

However, this does not seem to work. PlaySubtitle will not run when called, and I’m not sure why. When I comment out the “yield” line, it will run - but without the time and “click to continue” functionality.

Any help would be greatly appreciated. Thank you.

there’s also StopAllCoroutines()

regarding your code. you shoudl probably learn to use Invoke() first which is incredibly easier

unityGEMS.com for a vast explanation !