- Home /
Trouble loading sounds into array
Hey everyone. I'm trying to load a handful of sound files into an array via Javascript but the zeroth spot refuses to cooperate. I have all the sound file's names in a string array and load the audio clip array using a for loop. Initially I thought the problem was the for loop but tried manually loading individual slots and found that all besides the very first slot will load via the script. If I try dragging and dropping a file from the resources folder into the array during runtime it works fine but players obviously can't do that. Has anybody ever run into a problem like this before or does anybody have a possible solution?
Here's basically what the JS looks like;
var sound = new AudioClip[6];
var sfile : String[] = ["Chime_01","Fireball_01","Roar_01","Snarl_01","Stomp_01","Sword_01"];
then later in Start()...
for ( var j : int = 0; j < 6; j++){
sound[j] = Resources.Load(sfile[j]);
Then in inspector I always see this...
Sound
size 6
Element 0 None(Audio Clip)
Element 1 Fireball_01
Element 2 Roar_01
Element 3 Snarl_01
Element 4 Stomp_01
Element 5 Sword_01
bizarre... Anyways thanks for your help in advance.
Answer by aldonaletto · Jun 07, 2011 at 02:36 AM
I've tried the same thing, but it worked for me. I had problems initially because my sound files were in the wrong folder, so all elements of sound[] became empty - but I had no error messages. When I finnally put the files in the right folder, it worked for all elements of the array. Could the first file name of your list be misspelled, or in a folder other than Resources?
Since entry one to five are placed in slot one to five, the only obvious reason for it 'skipping' entry zero is because the name is indeed misspelled or it's not in the correct folder. I'm assu$$anonymous$$g you have a closing } but you just didn't copy/paste it.
You know, I thought so too and tried rena$$anonymous$$g the file and moving it around but that didn't seem to work. It does the same thing regardless of the file I'm trying to put in it. I'm going to try rena$$anonymous$$g the array and see if that fixes it or not.
I also just straight up tried sound[0] = Resources.Load(filename here); and had the same problem. so weird...
So you're saying that if, in the sfile:String[] array you switch "Chime_1" and "Fireball_1" it will still skip over entry zero in the sound:AudioClip[] array and not the first entry which would be expected if it couldn't find "Chime_1"?
That is correct. I'm about to just delete the audioclip array and re declare it with a different name.
Your answer
Follow this Question
Related Questions
Random Array of sounds for Random Game Object 1 Answer
Convert Array into One String (Js) 1 Answer
Help me Convert JS to C# 2 Answers
+= doesn't work with a 2d array in JS 1 Answer
Adding an object to an array of custom objects (JS) 1 Answer