I’m working on a rubiks cube algorithm trainer project on unity and I’m trying to create json files to store different collections of algorithms.
When I create the file i immediatly try to update my collections but I then get a NullReferenceException.
Here is all the code that is related to my issue.
public void AddCollection(string collectionName)
{
string tempPath = new string(path + "/" + collectionName + ".txt");
if (!File.Exists(tempPath))
{
File.WriteAllText(tempPath, GetBaseFileCollection(collectionName));
}
algManager.UpdateCollections();
UpdateDropDown();
}
public string GetBaseFileCollection(string collectionName)
{
string temp = "{rnt"collectionName" : "" + collectionName + ""rn}";
return temp;
}
//algManager
public void UpdateCollections()
{
algorithms.Clear();
info = new DirectoryInfo(path);
var fileInfo = info.GetFiles("*.txt");
foreach (FileInfo file in fileInfo)
{
var textFile = Resources.Load<TextAsset>(file.Name.Replace(".txt", ""));
algorithms.Add(JsonConvert.DeserializeObject<AlgorithmCollection>(textFile.text));
//the error occurs on this line ^
}
And here is my AlgorithmCollection class that the json files deserializes into
public class AlgorithmCollection
{
public string collectionName;
[SerializeField]
public List<AlgorithmBase> algorithms;
}
[System.Serializable]
public class AlgorithmBase
{
public string algorithmName;
public List<string> algorithm;
public int cubeSize;
[SerializeField]
public List<Mover.Face> tilesColor;
}
I tried using VS2022 debug function and when entering the foreach UpdateCollection i do have the file but var textFile is null after Resources.Load.
BoringPlatypus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.