In my game, I’m making a dialogue system that displays text every time a certain button is pressed, but it doesn’t work for one of the method calls. Two methods, ReadLine with three arguments (method RL3) and ReadLine with two arguments (method RL2), are available to display text, which is typed out like a typewriter. Everything works fine, but the first time method RL2 is called after method RL3, the text does not appear as though it was typed by a typewriter. When method RL2 is called before method RL3, the text appears correctly. If both methods are called separately, the text appears correctly. The methods are pretty much the same except for where the text is going and how many arguments they have. What am I missing here?
Dialogue Script:
public class DialogueScript : MonoBehaviour
{
private Image dialogueBox;
private TextMeshProUGUI nameBox;
private TextMeshProUGUI textBox;
private float textSpeed = 0.05f;
private int textIndex;
//Reference to dialogue box:
void Start()
{
dialogueBox = GameObject.Find("Overlay").transform.GetChild(5).GetComponent<Image>();
nameBox = dialogueBox.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
textBox = dialogueBox.transform.GetChild(1).GetComponent<TextMeshProUGUI>();
}
//Play dialogue:
void Update()
{
OpenDialogueBox();
ReadLine("Johnny", "I have a question!", 1);
ReadLine("Answer the question?", 2);
ReadLine("Are you sure?", 3);
ReadLine("Roderick", "Ask away.", 4);
CloseDialogueBox(5);
}
//Open dialogue box:
void OpenDialogueBox()
{
if (!dialogueBox.gameObject.activeSelf && Input.GetKeyDown(KeyCode.T))
{
dialogueBox.gameObject.SetActive(true);
nameBox.text = string.Empty;
textBox.text = string.Empty;
textIndex = 1;
}
}
//Close dialogue box:
void CloseDialogueBox(int closingIndex)
{
if (closingIndex == textIndex)
{
StopAllCoroutines();
dialogueBox.gameObject.SetActive(false);
}
}
//Types script to dialogue box:
void ReadLine(string name, string text, int num)
{
if (Input.GetKeyDown(KeyCode.T) && num == textIndex)
{
if (textBox.text == text)
{
textIndex++;
}
else if (textBox.text.Length < text.Length && textBox.text.Length > 0)
{
StopAllCoroutines();
nameBox.text = name;
textBox.text = text;
}
else
{
nameBox.text = string.Empty;
textBox.text = string.Empty;
StartCoroutine(TypeLine(name, text));
}
}
}
//Overload method for when there is no speaker:
void ReadLine(string text, int num)
{
if (Input.GetKeyDown(KeyCode.T) && num == textIndex)
{
if (nameBox.text == text)
{
textIndex++;
}
else if (nameBox.text.Length < text.Length && nameBox.text.Length > 0)
{
StopAllCoroutines();
nameBox.text = text;
textBox.text = string.Empty;
} else
{
nameBox.text = string.Empty;
textBox.text = string.Empty;
StartCoroutine(TypeLine(text, null));
}
}
}
//Typewriter effect:
IEnumerator TypeLine(string name, string text)
{
if (text == null)
{
foreach (char letter in name)
{
nameBox.text += letter;
yield return new WaitForSeconds(textSpeed);
}
}
else
{
nameBox.text = name;
foreach (char letter in text)
{
textBox.text += letter;
yield return new WaitForSeconds(textSpeed);
}
}
}
}