0

I am making a 2D game in which I wanted to attach an 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 my second audio file of the same format isn't working.

GameObject (called AudioManager) to which I have attached the Audio Source Component

UI Button Inspector

derHugo
  • 83,094
  • 9
  • 75
  • 115
Aravind
  • 31
  • 2
  • 11
  • 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. – Aravind Feb 24 '18 at 18:35
  • 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. – Aravind Feb 24 '18 at 18:36
  • 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. – Aravind Feb 24 '18 at 19:20
  • When you select the file within your project and play it within the inspector, does it play correct? If so, show us your code and if possible share information about your setup. If not, there's something wrong with your files. – Bart Feb 24 '18 at 23:05
  • @Programmer I tried doing that. But didn't work. – Aravind Feb 24 '18 at 23:13
  • @Bart Thanks for your response. Audio file plays properly within the inspector. I am not using a script to play the audio. Please see my first comment to know how i did it. Also, like I said, it works properly for every wav file that I tried except this one. – Aravind Feb 24 '18 at 23:17
  • Is it a very short file? (any chance you could share it?) Does changing import settings have any effect? – Bart Feb 24 '18 at 23:21
  • Yes. It is a very short file. Here is the link to that audio clip: http://soundimage.org/wp-content/uploads/2016/04/UI_Quirky22.mp3 – Aravind Feb 24 '18 at 23:24
  • @Programmer Thank you for responding again. I tried to do this through script. Did work for the first audio file again. But not for the second one. Also, I have attached the screenshot to the question description. Please see the edit. – Aravind Feb 25 '18 at 22:12
  • @Programmer I just realized that only 1/3rd of the audio is being played. When I press the UI button, it goes to another scene. And when this happens the audio gets cut. The audio doesn't get played fully. I guess this is what is happening. But why is this happening? – Aravind Feb 25 '18 at 22:49
  • Sorry about that. I didn't think that would make a difference since the first audio file worked properly. First Audio was heard because it was very very short. Therefore that Audio was able to be played fully before the next scene loaded. Anyway, thanks a lot for helping me! I got the issue resolved. I will post the answer here. – Aravind Feb 26 '18 at 11:04

1 Answers1

2

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!!

Aravind
  • 31
  • 2
  • 11