Trying to list names from text files in dropdown options.,Trying to list info from txt files.
I wanted to add a custom gear maker to my game. This uses text files to list attributes of the gear. I can create the files however when I try and pull from the mods directory in streaming assets to retrieve the gear name from within the text files I get the error "object reference not set to an instance of an object" for line 29. I want to add the names of the objects to a dropdown menu. My code is below. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO;
public class armouryChooser : MonoBehaviour
{
DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath + "/Mods/");
FileInfo[] info;
string[] generalMods = new string[100];
List<string> ArmouryMods, WeaponMods, EnemyMods;
public Dropdown weapon, armour;
// Start is called before the first frame update
private void Start()
{
print("Running Start");
int i = 0;
info = dir.GetFiles();
foreach (FileInfo f in info)
{
generalMods[i] = File.ReadAllText(info[i].ToString());
string[] temp = generalMods[i].Split(',');
switch (int.Parse(temp[0]))
{
case 0:
WeaponMods.Add(File.ReadAllText(info[i].ToString()));
break;
case 1:
ArmouryMods.Add(File.ReadAllText(info[i].ToString()));
break;
case 2:
EnemyMods.Add(File.ReadAllText(info[i].ToString()));
break;
}
i++;
print(WeaponMods[0]);
}
int w = 0;
foreach(string f in WeaponMods)
{
string[] temp = WeaponMods[w].Split(',');
weapon.options.Add (new Dropdown.OptionData() { text = (temp[w]) });
}
int a = 0;
foreach (string f in ArmouryMods)
{
string[] temp = ArmouryMods[a].Split(',');
armour.options.Add(new Dropdown.OptionData() { text = (temp[a]) });
}
armour.value = 1;
armour.value = 0;
weapon.value = 1;
weapon.value = 0;
}
}
Your answer
Follow this Question
Related Questions
How to add items to dropdown based on what's in a folder? 0 Answers
Disable a single Dropdown option. 2 Answers
Change graphics with Dropdown 0 Answers
DropDown with Text Input capabilities 0 Answers
Add buttons as options in dropdown 0 Answers