• 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 colinday · Sep 12, 2012 at 09:52 PM · prefabaudiosoundaudiosourcedisabled

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?

Comment
Add comment · Show 1
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 parmeshwar · Feb 20, 2013 at 10:11 AM 0
Share

audio.enabled = true;

add this line before audio.play();

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
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
0

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); }

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio source playing different soundfiles 0 Answers

shake the mouth painting when the user speaks 0 Answers

Best way to play many audio clips at the same time? 0 Answers

When is audio file loaded to memory 0 Answers

Is there a way to limit the output volume on an audio mixer group? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges