unity-game-engineaudioclipaudio-source

Why are some audio files not working when trying to play them in Unity?


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


Solution

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