- Home /
Scrolling Text StartCoroutine not working C#
Hey, im in the process of implementing scrolling text which makes use of a coroutine to pause between each letter thats added to a string, that string is then assigned to a GUIText. what should happen is the letters in the text appear 1 at a time with a delay between each. what is actually happening is similar except without the delay.
i know that this question is obviously asked a fair amount, but the answers i have found seem to be what i already have in place so my only though is maybe something else in my code is wrong, if anybody can point out the problem i would most appreciate it :)
using UnityEngine;
using System;
using System.Collections;
public class scrolltext : MonoBehaviour {
public string m_TextBody="";
public float m_PauseTime;
private char[] m_LetterArray;
private string m_TempBuffer="";
private GUIText m_GUI;
private int m_Counter;
public void Start () {
m_GUI = gameObject.guiText;
m_LetterArray = m_TextBody.ToCharArray();
m_TempBuffer = m_LetterArray[0].ToString();
m_Counter++;
}
public void OnGUI(){
m_GUI.text = m_TempBuffer;
}
public void Update(){
nextLetter();
}
private void nextLetter(){
if(m_Counter < m_LetterArray.Length){
m_TempBuffer += m_LetterArray[m_Counter];
StartCoroutine(WaitNumberOfSeconds(m_PauseTime));
m_Counter++;
}
}
private IEnumerator WaitNumberOfSeconds(float _val){
yield return new WaitForSeconds(_val);
}
}
Answer by aldonaletto · Mar 16, 2012 at 04:09 PM
I would change the logic a little: start with a blank string and m_Counter = 0, and place all the donkey work in the nextLetter function, which must be a coroutine.
When you start a coroutine, an instance of it is created and starts running; if you start this coroutine again before the previous one has finished, you get new instances running independently. To avoid this, you should use a boolean flag to tell when you can call nextLetter to add a new character - set it to false at the coroutine start, and set it to true when it finishes: ...
public void Start () {
m_GUI = gameObject.guiText;
m_LetterArray = m_TextBody.ToCharArray();
m_TempBuffer = ""; // start with blank string...
m_Counter = 0; // thus no characters added yet
}
bool canAdd = true; // only add new characters when this flag is true
public void Update(){
if (canAdd){ // if can add new character...
StartCoroutine(nextLetter()); // add it!
}
}
private IEnumerator nextLetter(){
canAdd = false; // disable nextLetter calls until the routine finishes
if (m_Counter < m_LetterArray.Length){ // if there's any character to add...
m_TempBuffer += m_LetterArray[m_Counter++]; // add it and advance counter...
m_GUI.text = m_TempBuffer; // update the GUIText...
yield return new WaitNumberOfSeconds(m_PauseTime); // and wait m_PauseTime...
canAdd = true; // re-enable nextLetter calls
}
}
Notice that when nextLetter is called and there are no more characters to add, canAdd never gets true again, what avoids further calls to nextLetter - an extra benefit!
thank you very much, few mistakes but you prob just copied and pasted my code :P very helpful and full of awesome-ness
Answer by syclamoth · Mar 16, 2012 at 02:20 PM
There are two problems here- the first is that the function 'nextLetter' needs to also be a coroutine if you want it to pause in the middle. However, because you don't want to start a new coroutine every frame (only when the counter should be incremented), the solution is a bit more subtle than that.
There are two possible ways to go here- you can move the
m_Counter++;
line down into the WaitForNumberOfSeconds coroutine- that would make sure it waits.
However, that might come off as bad form (because it doesn't reflect the name of the function very well). If doing it that way disgusts you too much, you could also split that off into two functions-
IEnumerator WaitAndIncrement(float waitTime)
{
yield return new StartCoroutine(WaitNumberOfSeconds(waitTime));
m_Counter++;
}
Then in 'nextLetter', just use
StartCoroutine(WaitAndIncrement(m_PauseTime));
didn't totally work correctly but still helpful in getting my head around the coroutine so thank you :)