- Home /
Animated text, word by word not letter by letter - coroutine
Hello, I am trying to use a coroutine to type word-by-word, below functions for letter by letter but I am not yet capable enough to split the string depending on the presence of a space so I can display each word individually. Word by word will solve an error I'm encountering with letter by letter.
tldr: How do I make this coroutine display word by word instead of letter by letter?
public TextMeshProUGUI textMeshProText;
public string contentText;
public float letterPause = 0.1f;
// Use this for initialization
void Start ()
{
string writeThis = contentText;
StartCoroutine(TypeSentence(writeThis));
}
IEnumerator TypeSentence(string sentence)
{
foreach (char letter in sentence.ToCharArray())
{
textMeshProText.text += letter;
yield return new WaitForSeconds(letterPause);
}
}
}
Answer by Hellium · Oct 22, 2017 at 11:57 AM
IEnumerator TypeSentence(string sentence)
{
string[] array = sentence.Split(' ');
textMeshProText.text = array[0];
for( int i = 1 ; i < array.Length ; ++ i)
{
yield return new WaitForSeconds(letterPause);
textMeshProText.text += " " + array[i];
}
}
Hellium, I love you - thank you so much for taking your time to do that.
Is it possible to still keep a space between the words? I am using it for maths and so it's not vital but I would implement it if possible.
Thanks again!
You're awesome, I hugely appreciate your help with this (I was very stuck) - have an excellent day!
Your answer
Follow this Question
Related Questions
How to use Coroutines in C++/CLI? 1 Answer
Generating Objects at High Speeds 0 Answers
coroutine in update method 1 Answer