1
\$\begingroup\$

I am making a 2D game in which I wanted to attach audio file to a button. I was able to do this successfully. But later I felt like changing that sound. So I simply replaced it with another audio file. Then it stopped working! When I again attached the first audio file instead of second one, it worked again.

My setup is as follows: The file format is wav. I created an empty gameobject and attached an Audio Source component to it. Assigned an audio clip to it. Unchecked "Play on Awake". Went to the OnClick() section of the UI Button. Clicked the plus sign to add an item to the list. Assigned the Audio Source object to the object field. Chose AudioSource.Play in the dropdown.

I wanted to know why is my second audio file of the same format not working!

\$\endgroup\$
  • 2
    \$\begingroup\$ Lacking any information about the formats, encoding, bitrate, etc. of each file, I can only speculate that the second file is cursed and you should consult an exorcist. \$\endgroup\$ – DMGregory Feb 24 '18 at 21:13
  • \$\begingroup\$ Format is wav. I created an empty gameobject and attached an Audio Source component to it. Assigned an audio clip to it. Unchecked "Play on Awake". Went to the OnClick() section of the UI Button. Clicked the plus sign to add an item to the list. Assigned the Audio Source object to the object field. Chose AudioSource.Play in the dropdown. \$\endgroup\$ – Aravind Feb 24 '18 at 23:08
  • \$\begingroup\$ It worked for some audio files. But for some it didn't. I couldn't find the difference between those audio files. Both had same format and similar size.Also, none of the mp3 files are working. The second audio file was also an mp3 file initially. But I converted it to wav using an audio converter before importing to unity. Is that the reason why it is not working? But when I checked the Unity documentation, it was written that Unity supports mp3, wav and many other formats. \$\endgroup\$ – Aravind Feb 24 '18 at 23:09
1
\$\begingroup\$

I got my issue resolved. The actual problem was that, when I click on the UI button, another scene loads. And when this happens, the audio which was supposed to be played while I click the button, gets cut off! Therefore, only those audios which are very very short in length would be heard. This is the reason why the second audio was not heard. In order to fix this, put the AudioSource on a game object with a script that contains this:

DontDestroyOnLoad( gameObject );

http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Thanks a lot to everyone who tried to help me!!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.