Populating a dropdown with options using the 1st line of txt files
This is in C#
I want to create an option in a dropdown for each txt file, using the first line of the txt file as the name of the option. Basically, I start out by counting how many txt files are in the directory, then I set the 'file to be read' as Y. I read the first line of Y, add the dropdown option with the name of the first line and +1 to Y. As you can see it's supposed to loop this for as many times as there are txt files. It is only adding the 1st txt file's line to the dropdown but not the 2nd txt file's line. Inspector shows Y changes to 2 and txtfileCount changes to 2 also, which means it's not changing the directory?? Help plz =)
Here is the code:
public void InitializeWorkoutsList()//populate the dropdown menu using saved workouts(txt files)
{
var txtlist = Directory.GetFiles(@"C:\Users\Kyle\Desktop\UnityProjects\WorkoutManager\WorkoutManager\", "*.txt");
txtfileCount = txtlist.Length;
while (y < txtfileCount) {
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Kyle\Desktop\UnityProjects\WorkoutManager\WorkoutManager\" + y + ".txt");
if ((line = file.ReadLine()) != null)
{
switch (lineNumber)
{
case 0://workoutname
Dropdownworkouts.options.Add(new Dropdown.OptionData() { text = line });
y++;
break;
}
}
}
}
Answer by knobblez · Nov 11, 2018 at 11:50 PM
Fixed it myself. very easy
while (y < txtfileCount) {
is supposed to be
while (y <= txtfileCount) {