I want to create an autotyping effect for my dialogue. Right now i am storing my dialogue in a nodebased editor. When i now want to let the text type out slowly it will make it appear 2 times, with only the second one being typed out.
public class DialogController : MonoBehaviour
{
[SerializeField] private GameObject dialogUI;
[Header("Text")]
[SerializeField] private Text textName;
[SerializeField] private Text textBox;
[SerializeField] public float typingSpeed;
private void Awake()
{
ShowDialogUI(false);
buttons.Add(button01);
buttons.Add(button02);
buttons.Add(button03);
buttonsTexts.Add(buttonText01);
buttonsTexts.Add(buttonText02);
buttonsTexts.Add(buttonText03);
}
public void ShowDialogUI(bool show)
{
dialogUI.SetActive(show);
}
//loading in the text
//TODO typing in the text when loading it in.
public void SetText(string text)
{
textBox.StartCoroutine(Type());
IEnumerator Type()
{
foreach (char letter in text.ToCharArray())
{
textBox.text = text = letter;
yield return new WaitForSeconds(typingSpeed);
}
}
}
public void SetName(string text)
{
textName.text = text;
}
}
CodePudding user response:
Because you wrote this:
textBox.text = text = letter;
The full text will always be there completely, and one letter of the full text will be added one by one. Change it to
textBox.text = letter;