Hi! I’m using Text Mesh Pro for the first time.
I need a character to talk and say different sentences when the player press space on the keyboard.
Everithing seems to work the only problem is I just get the first sentence.
My function NextSentence is not working at all!
I post my code :
public TextMeshProUGUI TextDisplay;
public string Dialogue;
int i = 0;
private void Start()
{
StartCoroutine(ScrollText());
}
IEnumerator ScrollText()
{
foreach (char letter in Dialogue*.ToCharArray())*
{ TextDisplay.text += letter; yield return new WaitForSeconds(0.02f); } NextSentence(); } public void NextSentence() { if (Input.GetKeyDown(“space”)) { i++; TextDisplay.text += “”; StartCoroutine(ScrollText()); }
else TextDisplay.text = “”; } Thank you so much for your help!
@ElaGingy its probably because NextSentence only gets called once at then end of the Coroutine. If the player isn’t already holding down space at that exact moment it would call the else and then just finish.
You need to be checking for that space bar in every frame, so add NextSentence() to update.
The problem I see with that is every frame that space bar isn’t held down it would clear the text every frame, also if the player pressed space while the writing was currently been displayed it would start again. Also it would throw an error when i gets higher than your array size.
Ive made some corrections and tested this, it will work.
public TextMeshProUGUI TextDisplay;
public string[] Dialogue;
int i = 0;
bool readyForNextLine = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine(ScrollText());
}
// Update is called once per frame
void Update()
{
NextSentence();
}
IEnumerator ScrollText()
{
TextDisplay.text = "";
foreach (char letter in Dialogue*.ToCharArray())*