For my code I have a text file which looks similar like this:
- Step1 - * First Move - * Second Move
- Step2 - * First Move - * Second Move - * Third Move
- Step3 - * First Move
- Step4 - * First Move - * Second Move - * Third Move - * Fourth Move
For reading the text file I use the following code:
void Update()
{
readTextFileLines("fileName", "Step1")
}
[MenuItem("Tools/Read file")]
public void readTextFileLines(string fileName, string Step)
{
foreach(string line in System.IO.File.ReadAllLines("Assets/Resources/"+fileName+".txt))
{
if(line.Contains(Step))
{
string[] separator = { "-" };
string[] strList = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
//Now I want to show the string in a UI text field.
//For this I used the following part:
infoText.text = strList[1] + "\n" + strList[2]
// So if I have e.g. Step3 I will receive an error.
// If I have Step2 it will not show the "Third Move"
}
}
}
My question now is, how can I rewrite my code, that I do not get the index error?
My first test was with the following code:
for(int i = 1; i < strList.Length; i++)
{
informationText += strList[i] + "\n";
}
infoText.text = informationText;
but with this code it added way to many lines to my UI text field.
This is how my textfield looks like:

strListbefore trying to access items from it..readTextFileLinesinUpdate()informationTextvariable declared outside of the loop and it gets bigger each iteration?