- Home /
Starting and stopping Coroutines
Hi all,
I'm trying to make a subtitle system where:
text is displayed
for an amount of time
after which, it disappears
OR the player can click to make it disappear
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.
Thanks very much for the link - good info. I came to realise that I was so hooked on the idea of using Coroutines, I didn't even think of any other way.
I don't see Invoke doing what I want to do. I ended up making a simple timer and putting all the subtitles in a queue, and managing those things ins$$anonymous$$d - which worked beautifully. When the time limit is reached OR when the player clicks, it moves to the next subtitle in the queue; if none, don't show subtitles.
Question now is, how do I close this question without an answer?
i hope it helps in some way, do not hesitate to ask other new questions ! i made an answer , cheers !
Answer by Fattie · Oct 24, 2012 at 06:31 AM
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 !
Your answer
Follow this Question
Related Questions
Time Manager Class Implementation Like Coroutine in Unity 1 Answer
RPG Action Progress Bar always same speed 1 Answer
Need help developing Damage Over Time mechanic 0 Answers
Timings and synchronization difference after build 0 Answers
(Rapid fire) How to accurately wait for a very short amount of time? 4 Answers