I don't want the player to spam the continueButtonSE hence, I only want it to appear after the dialogue has finished.
It works in the first index but for the next element, the continueButton does not appear. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DialogueSE : MonoBehaviour
{
public TextMeshProUGUI textDisplaySE;
public string[] sentencesSE;
private int index;
public float typingSpeedSE;
public GameObject continueButtonSE;
void Start()
{
StartCoroutine(Type());
}
void Update()
{
if(textDisplaySE.text == sentencesSE[index])
{
continueButtonSE.SetActive(true);
}
}
IEnumerator Type()
{
foreach (char letter in sentencesSE[index].ToCharArray())
{
textDisplaySE.text += letter;
yield return new WaitForSeconds(typingSpeedSE);
}
}
public void NextSentenceSE()
{
continueButtonSE.SetActive(false);
if (index < sentencesSE.Length - 1)
{
index++;
textDisplaySE.text = " ";
StartCoroutine(Type());
}
else
{
textDisplaySE.text = " ";
continueButtonSE.SetActive(false);
}
}
}
I've disabled the continueButtonSE from the start so that it can only appear once sentencesSE[index] is done appearing.
You are prepending a space when you say textDisplaySE.text = " "; and then you add things in the coroutine therefore if(textDisplaySE.text == sentencesSE[index]) is never true because your in the second case textDisplaySE.text is actually [space]test2 instead of just test2 which is what sentencesSE[index].
On line 31 instead you can initialize to textDisplaySE.text = string.Empty; to not have any whitespace.
In addition to the explanation of your issue from this answer
Why do you need the check at all?
You could simply let your Coroutine handle the end of the typing and enabling the continue button like e.g.
IEnumerator Type()
{
foreach (char letter in sentencesSE[index].ToCharArray())
{
textDisplaySE.text += letter;
yield return new WaitForSeconds(typingSpeedSE);
}
if (index < sentencesSE.Length - 1)
{
continueButtonSE.SetActive(true);
}
}
public void NextSentenceSE()
{
continueButtonSE.SetActive(false);
if (index < sentencesSE.Length - 1)
{
index++;
textDisplaySE.text = "";
StartCoroutine(Type());
}
else
{
textDisplaySE.text = "";
Debug.Log("Reached end of dialog");
}
}
This way you don't need the Update method and string compare at all and wouldn't even get into the issue with the prepend space character ;)