- Home /
Play audio using prefab warnings
I have a bunch of UI button prefabs in my project. At the prefab level I'd like to setup each of the "pressed" sounds for all of the buttons to refer to the same audio source prefab, this way I can change the audio clip in that single audio source and all of my button pressed sounds will use the new sound.
So, my buttons have a field
public AudioSource PressedSoundPrefab;
In the project inspector I set PressedSoundPrefab for the buttons to the correct Prefab
In the runtime, when I say PlayAudio() I get the following warning and the sound sometimes does not play properly (this is a 2D sound):
Can not play a disabled audio source
UnityEngine.AudioSource:PlayOneShot(AudioClip)
Now, I understand that the prefab audio source linked to by all the buttons is in fact a prefab, and that it is really a "disabled" game object ... so I understand why there is a warning. I've also been reading that playing a disable audio source for a 2D sound is possible (no position is needed), so that is perhaps why it partially works (except the audio doesn't always play right), playing a 3D sound from a audio source prefab of course does not really make sense since the prefab is not in the world and 3D sounds need an actual instantiated position.
The fix apparently is to make all the buttons not refer to an audio source prefab, but an instantiated game object based on that prefab that is in the actual scene.
This is incredibly inconvenient and makes sharing UI buttons between different scenes very very difficult as I have to instance the button sound in each scene, and then for each button in each scene set the pressed sound to the prefab object that is instantiated in the scene.
I've explored the options of not using an audio source ... but instead using an "AudioClip" field, which I can do, but then I lose the nice little controls for volume of the button press that are present on the AudioSource. I can probably come up with some hack via code to go find a tagged audio source in a collection of instanced shared sounds ... but it really seems like I should just be able to do this via prefabs in the project inspector for all my prefabs and UI sounds.
Surely I'm just missing how to properly organize my UI data to play well with audio. Any suggestions out there?
audio.enabled = true;
add this line before audio.play();
Answer by melenlu · May 30, 2015 at 11:29 AM
In Unity 5 you should instantiate your prefab audio. On start, for example public void Start() {
MyAudio = Instantiate(MyAudio);
}
After being instantiated it becomes active and enabled.
Answer by sompod44 · May 01, 2020 at 10:34 AM
Hello Coders!!! This problem occur when we try to play a sound from a prefabs and that moment we wanna destroy the gameobject. So all things are happen in a min time so that we thing maybe sound clips are not playing. so we need some time for playing the audio clip for playing then need gameobject destroy. So the simply code is : Sound.play(); while (!Sound.isPlaying) { Destroy(gameObject); }