• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by murkantilism · Feb 19, 2014 at 05:56 AM · audioarraysaudioclipmp3audioclips array

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.

Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Feb 19, 2014 at 06:02 AM 0
Share

You should make your audio clip array public to see if the method returns anything.

You could also try:

  pickupMP3s = Resources.LoadAll("Level 4/Level 4 Pickup Tracks") as AudioClip[];

You should avoid space in a name, use a _ instead.

avatar image murkantilism · Feb 19, 2014 at 06:12 AM 0
Share

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 = pickupMP3s[i]; This thread does it slightly differently, not sure if that would make a difference though.

avatar image fafase · Feb 19, 2014 at 06:14 AM 0
Share

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.

avatar image murkantilism · Feb 19, 2014 at 06:15 AM 0
Share

Yes it's all in Assets/Resources/Level4

avatar image murkantilism · Feb 19, 2014 at 06:34 AM 0
Share

Ohhh of course! That was a silly mistake on my part.

It's actually NotesArray[i].audio.clip = pickupMP3[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 =)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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);
Comment
murkantilism
stacker3d
MiraiTunga

People who like this

3 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image murkantilism · Feb 19, 2014 at 06:28 AM 0
Share

Oh brilliant!!!! That fixed some problems, the pickupMP3s array is now populated with all of the AudioClips!

However, it's still throwing the same NullReferenceExceptions at line: note.audio.clip = pickupMP3s[i];

avatar image fafase · Feb 19, 2014 at 06:30 AM 0
Share

Yes coz where is it from? If it comes from the NotesArray you need:

 NotesArray[i].note.audio.clip = pickupMP3[i];
avatar image fafase · Feb 19, 2014 at 06:33 AM 0
Share

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.

avatar image murkantilism · Feb 19, 2014 at 06:43 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Existing solution for load MP3 from disc ? 2 Answers

Why Won't My Audioclip Loop Seamlessly? 1 Answer

Which AudioClip formats will decompress using hardware? 0 Answers

Check scilence in audio clip using FFT. 0 Answers

Play MPEG from web options 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges