Hey forum. I’m trying to get my Scriptable Object data for all the songs to load asynchronously.
In the Debug.Log, all the try return a message with the correct songData’s name in Lime.
But when I add the song to the actual List, all of them are null, so I just end up with 18 empty data slots and when I Debug it, just a regular 'ol NullReferenceException.
This is what I had first. Seems to be incredibly inefficient.
allSongData = Resources.LoadAll<SongData>("Audio/SongData/").ToList();
This is what I’m trying:
private IEnumerator LoadAllSongs()
{
allSongData = new List<SongData>();
DirectoryInfo dir = new DirectoryInfo($"{Application.dataPath}/Resources/Audio/SongData");
FileInfo[] info = dir.GetFiles("*.asset");
foreach (FileInfo file in info)
{
//Since LoadAsync is already searching from a resources folder, we leave out Application.datapath ect.
var request = Resources.LoadAsync($"Audio/SongData/{file.Name}", typeof(SongData));
Debug.Log($"<color=grey>Looking for {file.Name}...</color>");
while (!request.isDone)
yield return null;
try
{
SongData song = request.asset as SongData;
allSongData.Add(song);
//When I put song.name instead of file.Name it will return the red catch event.
Debug.Log($"<color=lime>{song.name} was found!.</color>{Environment.NewLine}{file.FullName}");
}
catch
{
Debug.Log($"<color=red>{file.Name} wasn't found.</color>");
}
}
}
I’m using Unity 2019.4.38f1 btw.
I think the issue is var request = Resources.LoadAsync($“Audio/SongData/{file.Name}”, typeof(SongData));
Does anyone have an idea on what I should be doing here? Thanks in advance!