- Home /
How to make a 3d text writeOut method?
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);
}
}
1) 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.
2) 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?
Answer by robertbu · Feb 09, 2014 at 04:31 AM
I could not see anything wrong with your code, so I dropped it into a class and tested it. It worked fine. Only one of each letter and the timing was even.
Multiple letters and uneven timing would happen if 1) you launched two coroutines, or 2) you have the same script attached two times.
Take a look at where you call StartCoroutine(). Also use the search box in the upper right of the hierarchy to see if you have multiple instances of the script.
$$anonymous$$y problem is that my character model is made up of 2 colliders, not just 1, so it's being called once for each of my player's colliders entering. I completely forgot about that! Tyvm :)