- Home /
How do I assign audio files to an AudioClip[] array?
I'm trying to assign audio files (wav) to an array so that the script can play the music for the level I'm in and so on, and to make other objects/scripts check which music is playing. Here is my code so far (C#):
//Declaring the variable
public AudioClip[] list;
void Start ()
{
//Loading the items into the array
list = new AudioClip[]{(AudioClip)Resources.Load("Sound/Music/intro.wav"),
(AudioClip)Resources.Load("Sound/Music/level1.wav"),
(AudioClip)Resources.Load("Sound/Music/level2.wav"),
(AudioClip)Resources.Load("Sound/Music/level3.wav")};
}
But when I play my scene the only thing happening is that I see the array in the inspector grow up to make place for four items, which is right, but the slots are empty (music wasn't put in them) What's wrong with the code?
Thanks in advance for your help.
Resources.Load loads from the Assets/Resources/ folder in your project, is the Sound folder inside of your Resources folder? It might not be finding those audio files.
Ahh, thank you. For some reason I thought Resources.Load loads from the Assets as the root folder.
Answer by Dblfstr · Mar 20, 2014 at 02:08 PM
@stevethorne is correct. Your audio files should be inside of a folder named Resources, in order for Resources.Load to work.
Also, to load resources this way, you must leave off the extension of the file i.e. "level1.wav" would be "level1". More so, if your path is Assets/Resources/Sound Then you would do
Resources.Load("Sound/Level1");
Here is some info on resources.Load https://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
And here for nested folders: http://answers.unity3d.com/questions/199313/resourcesload-folders-path.html
And remember, everything in the Resources folder will be included in the build, even if it is not used.
Thanks all of you, it now works perfectly after combining your answers.
Is there an updated version of this? I just tried this and it says its an outdated script.
Your answer
