0

I'm trying to load all my audiofiles to find out how many files I got:

private int GetLength()
{
    AudioClip[] array = Resources.LoadAll("Audio/Melody/Test", typeof(AudioClip)) as AudioClip[];
    Debug.Log("Length: " + array.Length); // Gets Error here!
    return array.Length;
}

My Resourcesfolder look like this: Assets/Resources/Audio/Melody/Test/12 audio files

Error: NullReferenceException: Object reference not set to an instance of an object MelodyCreator.GetLength () (at Assets/Scripts/MelodyCreator.cs:24)

So my question is:

How do I get this piece of code to return the total amount of audioclips I have in a folder?

Spend hours looking for the answer but could not find a answer that worked for me. I Hope someone is able to help me.

Humeroush
  • 3
  • 3
  • Make sure to actually describe the issue with your current code in your future questions. It's not clear what your issue is but audio files are loaded as `AudioClip` not `AudioSource`. The `AudioSource` component is used to play the `AudioClip` after loading it. – Programmer Jun 23 '18 at 12:17
  • @Programmer Thx for the help. My problem was just making sure the code was correct! AudioClip[] array = Resources.LoadAll("Audio/Melody/Test") as AudioClip[]; – Humeroush Jun 23 '18 at 19:25
  • Do not modify your answer to remove your original code. Add EDIT then add your new code below it. While at it, you may want to add a screenshot of where you put the audiofiles – Programmer Jun 23 '18 at 19:43

0 Answers0