How to read a specific line from a text file?

im making subtitles for my game and i have already written code for it show the letters 1 by 1 but the problem is that it writes the entire text file and i want to only write a specific line from it.

here is my code so far:

using UnityEngine;
using TMPro;
using System;

public class SubtitleController : MonoBehaviour
{

    private TextMeshProUGUI Subtitle;
    public float speed = 0.5f;

    public TextAsset SubtitleFile;

    // Start is called before the first frame update
    void Start()
    {
        Subtitle = GetComponent<TextMeshProUGUI>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            StartCoroutine("Subtitle1");
        }
    }

    IEnumerator Subtitle1() 
    {
        foreach (Char sub in SubtitleFile.ToString())
        {
            Subtitle.text += sub;
            yield return new WaitForSeconds(speed);
        }
    }
}

Hi @kalimbadude

Without seeing your dialogue file format, I can give you generic example only.

Say you have your dialogue lines for a single character in one text file, like this:

Hey there, adventurer! What brings you to my little shop today?
Take your time, friend.
Greetings, traveler!

Then you only need to read this text file, either for example by using StreamReader or File class, or simply by using text asset field in your script.

Here is an example how this could look, first you have a field for your text file, and then an array for the end result:

[SerializeField] TextAsset textAsset;
[SerializeField] string[] dialogueLines;

Then these fields would look like this, when you have assigned your text file:
image

Then you have the code to split the text file into separate strings:

public class ReadDialogue : MonoBehaviour
{
    [SerializeField] TextAsset textAsset;
    [SerializeField] string[] dialogueLines;

    void Start()
    {
        dialogueLines = SplitText(textAsset.text);
    }

    static string[] SplitText(string text)
    {
        if (text == null)
            return null;

        // split your text lines
        string[] lines = text.Split('\n');
        
        // test, print lines to console
        foreach (string line in lines)
        {
            Debug.Log(line);
        }
        return lines;
    }
}

When you call the SplitText, you’ll get your dialogue lines, which you can then feed to your system when required.

image