Adding Audio Clips to Array Through Script

I have all my audio files in the resources folder, under Sounds/BGM. I was playing around with a way to add each clip to my array but I’m completely stumped. Mainly on how do I get the count of the files from the resources folder to make the array’s length however many files are in the BGM folder?
This is what I have so far,

    private void Awake()
    {
        var musicClips = Resources.LoadAll<AudioClip>("Sounds/BGM");
        foreach (AudioClip file in musicClips)
        {
            for (int i = 0; i < musicClips.Length; i++)
            {
               
            }
        }
    }

I just A) Don’t know how to add said files to the array, and B) don’t know how to get the count of the files. It’s more of a workflow sort of thing, so that when I add music in the future I don’t have to move them into the array again, also in case anyone wants to add their own music :stuck_out_tongue:

I’m confused about a number of things here. But maybe the most confusing is… Resources.LoadAll returns you an array of AudioClips. Your musicClips variable is already an array of AudioClips containing as many items as there are audio clips Sounds/BGM. And you seem to be looping over it, so you clear know its length (musicClips.Length). So what’s the actual problem here? You already seem to have everything you’re asking for.

As an aside, depending on how much audio you’re putting into Resources/Sounds/BGM, that could have some negative memory impact on your game. Anything in Resources gets loaded into memory 100% of the time your game is running. That’s fine for certain things, but usual audio clips aren’t needed 100% of the time, and they can be fairly large. I personally wouldn’t put them into Resources, unless it’s just the clips that are used on every level. Otherwise, I’d dynamically load them using asset bundles / addressables only when those clips are needed. But this may not be an issue you need to worry about unless you’re using a ton of audio clips.

1 Like

Oh, I was not aware that it automatically created an array. I actually didn’t have the length set, I was just writing this script while playing around with some things that might work haha. Now I was able to replace that array and I think it’s working as intended haha.

I think it should be fine, it is about 15 songs that will be playing on every level, most at random. It’s a simulation game. A few songs will only be played under certain conditions, but for the most part all songs will be played in all levels. So, would it be better to put them in a separate folder? Or is it fine?

That’s probably fine. If you were making something like Guitar Hero, with a large music library, you’d want to be more careful. But a few songs probably aren’t a big deal. But it’s something to think about down the road if you find the game’s process is holding on to a lot of memory for some reason. (Also, everything additional file in Resources slightly increases the time it takes for the game to start up, since it has to load all that stuff right from the beginning.)

1 Like

Oh, alright, thanks for the tips, it’s useful to know, especially since this won’t be my last project haha.