The following code works in a console app, but not in Unity 2020.3.2.f1
public RectTransform parent;
public RectTransform prefab;
public void Start()
{
Setup();
}
public void Setup()
{
StartCoroutine(Rolling());
}
public IEnumerator Rolling()
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\n.txt";
Dictionary<string, Medal> medals = LoadMedals(path);
foreach (KeyValuePair<string, Medal> entry in medals)
{
RectTransform medal = (RectTransform)Instantiate(prefab, parent);
medal.name = entry.Key;
medal.GetComponent<MedalDisplayer>().medal = entry.Value;
medal.GetComponent<MedalDisplayer>().Setup();
yield return null;
}
}
public Dictionary<string, Medal> LoadMedals(string csvFile)
{
Dictionary<string, Medal> returnDict = new Dictionary<string, Medal>();
string[] delimiters = new string[] { "," };
using (TextFieldParser parser = new TextFieldParser(csvFile))
{
parser.Delimiters = delimiters;
parser.TextFieldType = FieldType.Delimited;
parser.HasFieldsEnclosedInQuotes = true;
string[] split = null;
while (!parser.EndOfData)
{
split = parser.ReadFields();
returnDict.Add(split[0], new Medal(split[0], split[1], split[2], split[3], split[4], split[5], split[6], split[7], split[8]));
}
}
return returnDict;
}
Why is that?
I've added the Microsoft.VisualBasic.dll
to my unity project in a Assets / Plugins folder and I've checked the reference dependencies in the C# script. All should be fine.
My console output after running in Unity:
NullReferenceException: Object reference not set to an instance of an object
MedalManager.LoadMedals (System.String csvFile) (at Assets/MedalManager.cs:42)
MedalManager+d__4.MoveNext () (at Assets/MedalManager.cs:21)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
MedalManager:Setup() (at Assets/MedalManager.cs:16)
MedalManager:Start() (at Assets/MedalManager.cs:12)