Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

How to start a sound not from it's start ? 2 Answers

footstep sound 1 Answer

One AudioSource on player or one assigned to every collectable (hundreds)? 1 Answer

Playing a sound clip from another object 1 Answer

Sound Clip isn't playing when triggered. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges