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?
CodePudding user response:
Acording to the docs you should be able to set it using _Text.text = _Subtitles;
CodePudding user response:
There are three things:
- You need to access the text property of the Text class to be able to pass on the string
- You need to select an element from your string array, since Text.text only accepts strings not string arrays
- 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);
}
}
CodePudding user response:
the method text is only a string but _Subtitles is an array of strings. You need to build a single string with all element of _Subtitles.