- Home /
Dialogue Text becomes gibberish if activate too quickly
I am trying to create a dialogue system where the dialogue will continue if the player press E. However, if the player press E too quickly, the dialogue text will become gibberish as shown in the screenshot. Please advise. Thanks
IEnumerator Type() { foreach (char letter in sentences[index].ToCharArray()) { displayText.text += letter; yield return new WaitForSeconds(typingspeed); }
}
public void NextSentenceGuitar() { if (index < sentences.Length - 1) {
index++;
displayText.text = "";
StartCoroutine(Type());
}
else
{
displayText.text = "";
Canvas.SetActive(false);
}
}
Could it be activating before the arrays are populated? Or reading from uninitialized memory?
Answer by mchts · Mar 21, 2019 at 12:09 PM
I dont know what else happens when pressed on E (maybe problem lying under there) but i could suggest that to prevent user press E too early. Just define a bool e.g. typing
to keep track of your typing activity. Set it false initially and when typing coroutine starts set it true (or when you need it).
if(Input.GetKeyDown(KeyCode.E) && typing){
//do your stuff
}
Answer by timoffex · Mar 22, 2019 at 07:03 PM
This is happening because the previous Type() coroutine keeps going even if you start a new one. So if the user presses E twice in rapid succession, the first Type() method will start adding letters from sentences[0] and the second will start adding letters from sentences[1] simultaneously, causing the sentences to interleave with each other.
An appropriate solution is to stop the previous Type() coroutine before you start a new one, like so:
Coroutine typeCoroutine = null;
public void NextSentenceGuitar()
{
if (index < sentences.Length - 1)
{
index++;
displayText.text = "";
if (typeCoroutine != null)
StopCoroutine(typeCoroutine);
typeCoroutine = StartCoroutine(Type());
}
else
{
displayText.text = "";
Canvas.SetActive(false);
}
}
IEnumerator Type()
{
foreach (char letter in sentences[index].ToCharArray())
{
displayText.text += letter;
yield return new WaitForSeconds(typingspeed);
}
typeCoroutine = null;
}
Hi timoffex, thanks for spending the effort to put out the codes. I have copy pasted your code into Unity however it is still not working. =(
Coroutine typeCoroutine = null;
IEnumerator Type()
{
foreach (char letter in gameSentences[gameIndex].ToCharArray())
{
GameText.text += letter;
yield return new WaitForSeconds(GameTypingSpeed);
}
typeCoroutine = null;
}
public void GameStartSentence()
{
if (gameIndex < gameSentences.Length - 1)
{
gameIndex++;
GameText.text = "";
if (typeCoroutine != null)
StopCoroutine(typeCoroutine);
StartCoroutine(Type());
}
else
{
GameText.text = "";
GameCanvas.SetActive(false);
}
}
In GameStartSentence(), you're forgetting to set typeCoroutine:
typeCoroutine = StartCoroutine(Type());
Thanks timoffex, my codes are working now. I didnt get it to work in the first time but I changed it after I learn about the concept of typeCoroutine = null. Below is the code that worked for me. Thanks a lot man, youre a legend.
=D
Coroutine typeCoroutine = null; IEnumerator Type() {
foreach (char letter in gameSentences[gameIndex].ToCharArray())
{
GameText.text += letter;
yield return new WaitForSeconds(GameTypingSpeed);
}
typeCoroutine = null;
}
Answer by bentance · Mar 22, 2019 at 03:29 PM
Thanks mchts, I think you are right, the lines are executed if I hold down the E button for too long. I am not exactly sure what your solution meant but I will try to figure it now. Would appreciate if you can provide a bit more information.
I think i could help better if you provide some more detail about your code. Especially about what happens when you press $$anonymous$$ @timoffex's solution seems pretty reasonable too btw.
Yeah, I just learnt the concept of typeCoroutine = null and applied it. $$anonymous$$y codes are working now. Thanks for pointing out the error in the first place mchts.
Have a good one =D