[Solved] computer auto-typing and clearing text field for next message
So I have a program that types a text adventure, it types the text character by character. The problem is, the text does not clear when going to the next message, the previous message in the text field is still there when the coroutine types the next message. I have tried everything I could think of to solve the problem but there is no difference, no matter what I do.
Expected Result: I want the text field to clear before the coroutine TypeText() types on the line.
Result: The text is left over from the previous message, and the new message is written in over one letter at a time, and then the rest of the previous message gets deleted one character at a time:
What I have tried: I have tried emptying the text field by setting textComp = ""; before the next message;
I have tried emptying the string message by setting message to "";
I tried changing the message into a message array so it doesn't reference the same string;
I tried stopping the coroutine right before starting it back up again.
None of these methods changed the result at all. I have run out of ideas of what to do to fix this.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextController : MonoBehaviour {
Coroutine lastTypeRoutine = null;
private enum States {intro, cell, mattress_0, mirror_0, cell_mirror, matress_1, lock_1, freedom};
private States myState;
string message="";
Text textComp;
// Use this for initialization
void Start()
{
myState = States.intro;
textComp = GetComponent<Text>();
state_intro();
}
void Update()
{
if(myState == States.intro)
{
state_intro();
if (Input.GetKeyDown(KeyCode.Return))
{
myState = States.cell;
}
}
if(myState == States.cell)
{
state_cell();
}
if(myState == States.mattress_0)
{
state_mattressLook();
}
}
void state_intro()
{
message = "Welcome to prison! hit ENTER to continue!";
textComp.text = "";
lastTypeRoutine = StartCoroutine(TypeText());
}
void state_cell()
{
message = "You are trapped inside Fulsome Prison for a crime you didn't commit, your rival is seeking to have you killed within the confines of the prison walls. "
+ "You must find a way to escape your cell. Inside your cell is an old mattress, a broken mirror. The cell gate is locked from the outside. \n\n"
+ "Press RIGHT ARROW for Mattress, \n"
+ "Press FORWARD ARROW for Broken Mirror, \n"
+ "Press RIGHT ARROW for Cell Gate Lock."
;
textComp.text = "";
lastTypeRoutine = StartCoroutine(TypeText());
if (Input.GetKeyDown(KeyCode.RightArrow))
{
myState = States.mattress_0;
}
}
void state_mattressLook() {
message = "The mattess is worn and disgusting, stains and holes in it, they make you sleep on it without any sheets, there's a hole in the mattress.\n"
+ "Press UP ARROW to stick your hand in the mattress hole. \n"
+ "Press DOWN ARROW to return to where you were in your cell.";
textComp.text = "";
lastTypeRoutine = StartCoroutine(TypeText());
if (Input.GetKeyDown(KeyCode.DownArrow))
{
myState = States.cell;
}
}
IEnumerator TypeText()
{
foreach (char letter in message.ToCharArray())
{
textComp.text += letter;
yield return 0;
}
}
}
[SOLVED] I solved this by changing the code in the coroutine to clear the text field when a certain key was pressed. Notice the if statement that sets i = 0, and the clearing of the message box to "". Doing this ensured the box was empty on the key press. I added a few lines of code at the end so that I could hit enter and have the whole text appear.
IEnumerator ShowText()
{
if (Input.GetKeyDown(KeyCode.Return) && firstReturn == true || Input.GetKey(KeyCode.RightArrow) && firstReturn == true || Input.GetKey(KeyCode.LeftArrow) && firstReturn == true || Input.GetKey(KeyCode.RightArrow) && firstReturn == true || Input.GetKey(KeyCode.DownArrow)&& firstReturn == true)
{
i = 0;
message = "";
keyTimer = 0;
firstReturn = false;
print("firstReturn:" + firstReturn);
}
if (isTyped == false && firstReturn == false)
{
for (i=0; i < message.Length; i++)
{
currentText = message.Substring(0, i);
this.GetComponent<Text>().text = currentText;
if (Input.GetKeyDown(KeyCode.Return))
{
i = message.Length - 1;
currentText = message.Substring(0, i);
this.GetComponent<Text>().text = currentText;
}
if (i >= (message.Length - 1))
{
isTyped = true;
}
yield return 0;
}
}
}
Answer by Chinmay_Gawande · Jun 19, 2017 at 06:15 PM
Mate I have Same problem but did'nt Understood the code u typed can you please write it in simple manner
Your answer
Follow this Question
Related Questions
Issues with Scrolling Dialogue (repeating characters when printing text) 0 Answers
Coroutine does not stop? 1 Answer
How to change the text inside UI>Text 1 Answer
Unity 5 - Does anyone know how to scale the text in the UI? 1 Answer
CachedTextGenerator no longer has any character info in Unity 5.3 0 Answers