So I’d like to write a method to make text write out one character at a time like those old games from back in the day. I wrote a simple function for it, but ran into two problems that I don’t know how to fix! Here’s the code I wrote:
IEnumerator writeOut3DText(GameObject threeDText, string msg, float timeBetweenLetters)
{
threeDText.GetComponent<TextMesh>().text = "";
for(int i = 0; i < msg.Length; i++)
{
threeDText.GetComponent<TextMesh>().text += msg.Substring (i, 1);
yield return new WaitForSeconds(timeBetweenLetters);
}
}
-
Whenever the function ran, the first letter would be added, then every letter after that would be added TWICE, with the second being added almost immediately after the first (i.e. both copies of the same letter added in one loop, not once in each loop). I know from testing that this is not being called by the += nor the Substring call, but I don’t know how to fix it.
-
the yield return new WaitForSeconds(float time) function is really sporadic and unpredictable. It was very clear that the time between each letter appearing was NOT the same.
NOTE) I also tried turning the whole function into a normal void function and having an IEnumerator just for the wait method, and starting that coroutine in the normal method’s for loop, but that didn’t wait at all.
So how do I make my text write out one letter at a time, over a consistent amount of time, and without duplicating every letter after the first?