Dialog system - Spacebar continue

Hey everyone I got an issue with my code. I’m quite new to this so please don’t blame me, but i’m trying to make a dialog system for my game but just can’t seems to get it right. The first sentence shows up but it doesn’t go to the next sentence. Anyone have an answer for this??

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Dialog : MonoBehaviour
{
    public TextMeshProUGUI textDisplay;
    public string[] sentences;
    private int index;
    public float typingSpeed;

    void Start()
    {
        StartCoroutine(Type());
    }

    IEnumerator Type()
    {
        foreach(char letter in sentences[index].ToCharArray())
        {
            textDisplay.text += letter;
            yield return new WaitForSeconds(0.02f);
        }
    }

    public void NextSentence()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            index++;
            textDisplay.text = "";
            StartCoroutine(Type());
        }
    }
}

Hi @BIGSIPOFCOFFEE ,

your problem is that NextSentence() never gets called, ergo the if statement will never be checked.

You would have to call it the following way, as update() gets called every frame from unity:

public void Update(){
  if(Input.GetKeyDown(KeyCode.Space)){
    NextSentence();
  }
}

and

public void NextSentence(){
  index++;
  textDisplay.text="";
  StartCoroutine(Type());
}

Please be aware that fast space pressing would lead to multiple coroutines writing over each other.

Standard browser disclaimer, so be wary of typos etc

Hope this helps

Holy smokes it works, thank you so much! Been trying to figure it out for days. Really can’t thank you enough <3