- Home /
how to make letters appear one by one in a list
i have a dialogue manager in which i can assign the number of lines of text but i can't seem to find out how to make the letters appear one by one because the text is in a list . any ideas how to implement it here is my code.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DialogueSystem : MonoBehaviour
{
public static DialogueSystem Instance { get; set; }
public GameObject dialoguePanel;
public string npcName;
public List<string> dialogueLines = new List<string>();
Button continueButton;
Text dialogueText, nameText;
int dialogueIndex;
private void Awake()
{
dialoguePanel.SetActive(false);
continueButton = dialoguePanel.transform.Find("Continue").GetComponent<Button>();
dialogueText = dialoguePanel.transform.Find("Text").GetComponent<Text>();
nameText = dialoguePanel.transform.Find("Name").GetChild(0).GetComponent<Text>();
continueButton.onClick.AddListener(delegate { ContinueDialogue(); });
dialoguePanel.SetActive(false);
if(Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
public void AddNewDialogue(string[] lines, string npcName)
{
dialogueIndex = 0;
dialogueLines = new List<string>(lines.Length);
dialogueLines.AddRange(lines);
this.npcName = npcName;
Debug.Log(dialogueLines.Count);
CreateDialogue();
}
public void CreateDialogue()
{
dialogueText.text = dialogueLines[0];
nameText.text = npcName;
dialoguePanel.SetActive(true);
}
public void ContinueDialogue()
{
if(dialogueIndex < dialogueLines.Count-1)
{
dialogueIndex++;
dialogueText.text = dialogueLines[dialogueIndex];
}
else
{
dialoguePanel.SetActive(false);
}
}
}
and my second script for the npc .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPC : Interactable
{ public string[] dialogue; public string npcName;
public override void Interact();
{
DialogueSystem.Instance.AddNewDialogue(dialogue,npcName);
}
}
Answer by phouse2019 · Jun 06, 2019 at 03:57 PM
You are going to want a coroutine for this. When you create your dialogue, it populates a list, yes? You then immedistely read the first line after being created but only proceeded to the following lines on user input, correct?
So once it is created, you want to start a coroutine like this that will take your provided line, whether it is the first, second, or so on, and display each character one at a time.
{
Stop coroutine("BuildDialogue");
StartCoroutine(BuildDialogue(yourLine));
}
IEnumerator BuildDialogue(string line)
{
TextComponent.text = string.Empty;
For(int a = 0; a < line.length; a++)
{
Text component.text += line[a];
yield return new WaitForEndOfFrame();
}
}
Yes
Yes
i don't understand where to put { stop coroutine ("Buil....."); start coroutine(......); } i make it in update or ?
not working
IEnumerator BuildDialogue(string line) { TextComponent.text = string.Empty; --- underlined (TextCompnent.text) For(int a = 0; a < line.length; a++) ---- underlined everything except 0 { Text component.text += line[a]; ---- underlined (textComponent and line[a] yield return new WaitForEndOfFrame(); } }
Your answer