I'm trying to make a dialoge system for an undertale like game

Hello!
I am currently making an undertale like game, and I have some problems with the Dialogue.
For those of you who have never played undertale, basically the text writes itself out letter by letter, and then when you press a specific key(Usually either Enter or “Z”), it writes the next dialogue line.
And to add to it, if you press another button(Usually Shift or “X”) it writes it out all at once.
There’s also Icon changes but I don’t need help with those.
Basically, this is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class FlD : MonoBehaviour
{
    Text txt;
    string story;
    int curentline = 0;
    int texta = 0;
    public Animator huh;
    int a;
    int m = 0;
    public KeyCode ine;
    public KeyCode fin;
    public GameObject ime;
    public Animator anie;
    public int te = 1;
    public int weee;
    public int tele;
    public GameObject y;
    public int ret;
    public int Q;
    public float ag;
    public int Bef;
        public void Start()
    {
        ag = 0.09f;
        Sraer();
        ret = 2;
        }
    public void Sraer()
    {
        ime.SetActive(true);
        txt = GetComponent<Text>();
        story = txt.text;
        txt.text = "";
        StartCoroutine("PlayText");
    }
    public void Ruw()
    {
        if (Input.GetKeyDown(fin))
        {
            Bef = 9;
            Debug.Log("aaa");
        }
    }
    IEnumerator PlayText()
    {
        if (curentline == 0)
        {
       
            anie.SetFloat("Expression", 1);
//Here Im changing the sprite of the character
            story ="Example one ";
           

        }
        if (curentline == 1)
        {
            anie.SetFloat("Talking", 0);
            anie.SetFloat("Expression", 1);
            if (anie.GetFloat("Expression") == 1) {
            story = "Example 2, diffrent face";
            }           
//Here I'm Changing it again
        }

        foreach (char c in story)
        {
            if (Bef != 9)
            {
                texta++;
                anie.SetFloat("Talking", 1);
                txt.text += c;
                yield return new WaitForSeconds(ag);
                if (txt.text == story)
                {
                    anie.SetFloat("Talking", 0);
                    yield return new WaitUntil(() => Input.GetKeyDown(ine));
                    txt.text = "";
                    curentline++;
                    a = 1;
                    StopAllCoroutines();

                }
            }
            else if(Bef == 9){
                StopAllCoroutines();
            }
            if (Input.GetKeyDown(fin))
            {
                txt.text = story;
                break;
            }
        }


    }
    private void Update()
    {
        Ruw();
        if (Input.GetKeyDown(fin))
        {
            ag = 0;
        }
       
        if (te == 1)
        {
            ime.SetActive(true);
            Sraer();
            te = 2;
        }
        if (a == 1 && Q == 2)
        {
            a = 0;
            StartCoroutine("PlayText");
        }
        if (curentline > 2)
        {
            ime.SetActive(false);
        }
        if (curentline == 0)
        {
            weee = 4;

        }
        if (anie.GetFloat("Talking") == 1)
        {
            tele = 2;
//this value is used later on for changing the overworld sprite via another script
        }
        if (anie.GetFloat("Talking") == 0)
        {
            tele = 4;
//this value is used later on for changing the overworld sprite via another script
        }
    }
}

I know the code isn’t the cleanest, but I’m a beginner.

Anyhow, my problem is the fact that, while the letters do type out, it won’t type out the full Dialogue when I press the “fin” key(Currently set as “C”).

Any help is very much appreciated!
I’ve been struggling with this for a bit.
Thanks in Advance!

1 Like

I would have the fin key stop your coroutine and set the text field you are working with to the full string.

I have no idea what your code is doing. The variable names are quite ambiguous. I would recommend descriptive names and comments.

@IHaveNoIdeaYaKnow

This is a UI and general scripting question IMO. Don’t expect many answers here.

There are several tutorials on how to create typical text boxes where text gets printed one character at a time. One way to do this - you can use string class method to split your dialogue text into single characters and then start adding these one at time to your display text in a loop you run in a coroutine. Another way to do this is to use rich text. You can change text character alpha, using rich text tags, this way you can have all the text in your text box, so the text won’t move while it gets printed, as later characters are simply transparent.

If I got it right you also want to be able to print one line when the player presses a key? Like half of a single row is printed, and the player presses a key, that line will be completed in one frame and printing continues on the next line of dialogue box (if it has several lines)? This will be a bit harder, as if you are using Unity UI, I don’t think you have direct info in text itself about its layout to see how many characters actually fit into one line. And text characters are not monospace in most cases. There is at least Text.cachedTextGenerator with which you can get line end positions in your text string. I’m not sure if there is an easier way than that.

1 Like