- Home /
Loading MP3's into an Audio Array and assigning them to members of a GameObject Array
I'm trying to load an array of MP3 files from the Resources folder, and then assign each MP3 file to the Audio Clip value of a GameObject.
This list of GameObjects (referred to as "Notes") is sorted, and the MP3 files are already sorted manually (their names are in numerical order).
Below is my attempt, I've removed the sorting method SortNotes() to save space (it works perfectly fine, isn't the issue).
// An array of Notes
public GameObject[] NotesArray;
AudioClip[] pickupMP3s;
// Use this for initialization
void Start () {
// Grab and sort array of Notes
NotesArray = GameObject.FindGameObjectsWithTag("Note");
SortNotes();
// Grab a list of pick-up mp3 files
pickupMP3s = Resources.LoadAll("..\\Level 4\\Level 4 Pickup Tracks") as AudioClip[];
}
// Assign each mp3 file to a Note
void AssignNotes(){
for (int i = 0; i < NotesArray.Length; i++){
foreach (GameObject note in NotesArray){
try{
note.audio.clip = pickupMP3s[i];
Debug.Log(pickupMP3s[i]);
Debug.Log(note.audio.clip.name);
}catch(Exception e){
Debug.Log(e.ToString());
}
}
}
}
I get tons of NullReferenceException: Object reference not set to an instance of an object errors. They seem to stem from this line: note.audio.clip = pickupMP3s[i]; but I can't figure out what.
The objects already have Audio Sources attached.
You should make your audio clip array public to see if the method returns anything.
You could also try:
pickup$$anonymous$$P3s = Resources.LoadAll("Level 4/Level 4 Pickup Tracks") as AudioClip[];
You should avoid space in a name, use a _ ins$$anonymous$$d.
Hi fafse, thanks for the quick reply. Good call on making it public, I did and it's completely empty after re-running the script. It's not being populated with anything due to the errors.
As for changing the Resource.LoadAll string, it didn't make a difference =( I even replaced the spaces with underscores in the folder names.
I think I'm not assigning the audio clip properly at this line: note.audio.clip = pickup$$anonymous$$P3s[i]; This thread does it slightly differently, not sure if that would make a difference though.
Do you have it all in the Resources folder? In the Assets folder add a Resources folder and then add your Level4 folder.
Resources methods only work inside the Resources folder.
Ohhh of course! That was a silly mistake on my part.
It's actually NotesArray[i].audio.clip = pickup$$anonymous$$P3[i]; but yes, I understand what you meant.
Everything works perfectly now, thanks so much for your help fafase! You've really been a great help =)
Answer by fafase · Feb 19, 2014 at 06:18 AM
It could be an issue with the cast, you could try the generic version:
Resources.LoadAll<AudioClip>(path);
Oh brilliant!!!! That fixed some problems, the pickup$$anonymous$$P3s array is now populated with all of the AudioClips!
However, it's still throwing the same NullReferenceExceptions at line: note.audio.clip = pickup$$anonymous$$P3s[i];
Yes coz where is it from? If it comes from the NotesArray you need:
NotesArray[i].note.audio.clip = pickup$$anonymous$$P3[i];
Hold on actually that won't do since NotesArray is game object. I would think you have a script somewhere where you declared those notes. You need to get that component and assign it to the note.
Also why dont you just assign the audio clip to the slot for the note in a prefab? That would make it easier on you.
No it worked perfectly, I tested it by assigning dummy audio clips manually and then running the script, and it overwrites the audio clips with the correct clips from the Resources/Level4 folder.
It works because NotesArray is an Array of GameObjects, all of which already have AudioSources components.
I'm not assigning the audio clip in a prefab because for each level there are between 30-100 audio clips that need assignment. Doing so manually would take far too long, all of this work is for an automation tool I'm writing.