Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do set a Text from String?

I’m working on this subtitle system. And the way I have it set up is, I’m taking an array of strings, looping over them, then I’m trying to assign them to a text element. But that doesn’t seem to be possible.

public class SubtitleManager : MonoBehaviour
{
    public string[] _Subtitles;
    public Text _Text;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        foreach(string subtitle in _Subtitles)
        {
            StartCoroutine(SubtitleCoroutine());
        }
    }

    IEnumerator SubtitleCoroutine()
    {
        _Text=_Subtitles;
        yield return new WaitForSeconds(5);
    }
}

Am I going about this the wrong way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are three things:

  1. You need to access the text property of the Text class to be able to pass on the string
  2. You need to select an element from your string array, since Text.text only accepts strings not string arrays
  3. You need to start the loop inside of the corutine so that the texts are displayed sequentially
    // Update is called once per frame
    void Start()
    {
        StartCoroutine(SubtitleCoroutine());
    }

    IEnumerator SubtitleCoroutine()
    {
        foreach(string subtitle in _Subtitles)
        {
            _Text.text=subtitle;
            yield return new WaitForSeconds(5);
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading