Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by DillonRobinson · Jul 06, 2014 at 01:57 PM · audiosourceaudioclipresources.load

Overwrite an Object's AudioSource's AudioClip?

EDIT

So I sort of fixed it.

It was as simple as

 WWW www = new WWW(path + "music.wav");
         audio.clip = www.audioClip;

But...notice the .wav. Frankly, this is hugely disappointing, and very embarassing on Unity's end to see that they don't support the www loading of MP3 FILES. No excuses, Unity. Try harder. Guess my project is going to probably require a file conversion on the end-users end because this very, very simple functionality doesn't work for Windows executables. Sad, really.


Hi there! So I'm trying to add the ability for the player to use their own music file.

I want my script to look for a specific folder, and if there's a file in there, load it by my specific name. This should then be an audio.clip that overwrites the object's default music audio.clip.

the music is to be grabbed from the build's Resource folder, inside of a folder called "Music" that I will be adding to my distributed build.

Here's what I have:

 using UnityEngine;
 using System.Collections;
 //
 using System.IO; //in order to create folder
 //
 
 
 public class customMusic : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 

         
         //Make string of file path
         string path = "file://"+(Application.dataPath)+"/Resources/Music";
 
         //if a file exists in the Music Folder...
         if (Directory.GetFiles(path).Length > 0)
         {
             //then set the audio clip to be their music
                     audio.clip = (AudioClip)Resources.Load(path +"music.mp3");
             //audio.clip = Resources.Load(path + "music.mp3", typeof(AudioClip));
 
 
 
         };
 
 
     }
     //End Start
 
 
 
     // Update is called once per frame
     void Update () 
     {
     
     }
     //end Update
 
 }


I just tried it out. No errors, but no effect. I have a music file of mine renamed "music.mp3" and put into that build folder, but nothing happens. Am I incorrectly defining audio.clip? Should I be more specific about the audio source of my musicBox object that holds this script? Any help would be appreciated!

Now, this is separate from the Unityscript that I have on the same object that handles audio playing and pausing, so that's no issue. It works. But I just tested my script and it played the same default song that I have set in the inspector for the musicBox object, and not the (data path)Resources/Music/music.mp3 file.

I checked my output log, and I get this error:

NullReferenceException: Object reference not set to an instance of an object at customMusic.Start () [0x00000] in :0

Comment
Add comment · Show 2
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 meat5000 ♦ · Jul 06, 2014 at 07:59 AM 0
Share

path +"music.mp3"

/Resources/$$anonymous$$usicmusic.mp3

avatar image DillonRobinson · Jul 06, 2014 at 04:42 PM 0
Share

Oh my god...that's what I did there? I forgot a single slash. Let me take a look at this!

Edit: Nope :( I changed "path" to "file://"+(Application.dataPath)+"/Resources/$$anonymous$$usic/" - I added the slash - and it still loads the default audioclip.

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple AudioSources overwriting a looping AudioSource 1 Answer

Working Sound Clip not Audible on iOS Devices 0 Answers

How to get C# Array to Load into Audio Source 0 Answers

GetSpectrumData - what does it fill the samples argument with exactly? 0 Answers

AudioClip stuttering while loop is OFF and not using function Update? 1 Answer


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