Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by TheDogePwner · Sep 21, 2015 at 09:32 AM · c#audioaudiosource

Can Not Play An Audio Source Even Though It Plays On Awake

Hello,

Let me start off by saying yes, I know, there are a ton of other questions about this, but none of them have solved my problem.

Anyways, I have a prefab with an AudioSourcce component. It plays the sound on Awake, but whenever it is called through scripts, it doesn't play sound, but instead give a "Can not play a disabled audio source" warning.

Here is how it is called:

 if (Input.GetMouseButtonDown (0)) {
     CurrentWeapon.GetComponent<Weapon> ().Fire ();
 }

And here is the Fire() method:

 public void Fire () {
     GetComponent<AudioSource> ().clip = FiringSound;
         GetComponent<AudioSource>().Play();
 }
Comment
Add comment · Show 9
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 TheDogePwner · Sep 20, 2015 at 04:47 PM 0
Share

...bump...

avatar image Hellium · Sep 21, 2015 at 08:49 AM 0
Share

How about

 GetComponent<AudioSource> ().PlayOneShot( FiringSound );
avatar image TheDogePwner Hellium · Sep 21, 2015 at 11:23 PM 0
Share

Still does the same thing.

avatar image TheDogePwner · Sep 22, 2015 at 01:56 AM 0
Share

Anyone else can help?

avatar image Hellium · Sep 22, 2015 at 07:56 AM 0
Share

"Can not play a disabled audio source"

You haven't mentionned that before. The answer is pretty obvious then, isn't it ? Look for a call like this :

 GetComponent<AudioSource>().enabled = false ;
avatar image TheDogePwner Hellium · Sep 25, 2015 at 12:10 AM 0
Share

Doesn't seem to have that anywhere in my code. The code in the question is all of my code that references an AudioSource besides my CharacterController.

avatar image Hellium Hellium · Sep 25, 2015 at 07:18 AM 1
Share

Are you sure the component is enabled in the inspector ?

avatar image TheDogePwner Hellium · Sep 26, 2015 at 02:34 AM 0
Share

Yes, it is.

avatar image meat5000 ♦ · Oct 02, 2015 at 08:16 AM 0
Share

It Plays on Awake, you say.

This suggests that it is otherwise not enabled? You cant Play it if that is the case.

If it is enabled than make absolutely sure that you have dragged the correct audiosource into the relevant box.

Also note that a Prefab is not enabled as it is not instanced. After instantiation consider how you acquire the AudioSource reference.

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GundalfJunior · Mar 07, 2019 at 11:20 PM

I ran into the same problem today and noticed after a while, that I destroy the gameobject in the function, where I play the audio.

My conclusion was, that the gameobject gets destroyed, before the audio is played, even though my call for playing the audio came before destroying the object:

         AudioSource blockDestroyedSound = GetComponent<AudioSource>();
         blockDestroyedSound.Play();
 
         Destroy(gameObject);

The solution for this was to play the audio in a coroutine and destroy the gameobject after the audio was finished playing:

 private IEnumerator PlaySoundAndDestroyAfterwards()
     {
         AudioSource blockDestroyedSound = GetComponent<AudioSource>();
         blockDestroyedSound.Play();
 
         while (blockDestroyedSound.isPlaying)
         {
             yield return null;
         }
 
         Destroy(gameObject);
     }

@TheDogePwner

Comment
Add comment · Show 1 · 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 xproflipscarab · Jun 19, 2020 at 09:03 AM 0
Share

While its unclear if this helped the OP, This was defiantly an eye opener for me Thank you my dood!

avatar image
1

Answer by daniel-eherbert · Oct 02, 2015 at 07:44 AM

Is the AudioSource on the same object as the Weapon component?

Is CurrentWeapon a reference to the prefab itself, or the instantiated prefab?

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 abraham5361 · Sep 28, 2015 at 06:27 AM

Click Add Component > Audio > Audio Listener.You HAVE to have a audio listener in order to play the sound.

Also the Script,Audio Listener and Audio Source are in the same object.

Hope it works

Comment
Add comment · Show 1 · 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 TheDogePwner · Oct 02, 2015 at 12:09 AM 1
Share

The Player has an Audio Listener. Like I said, it works on Awake.

avatar image
0

Answer by jakejolli · Oct 02, 2015 at 12:21 AM

Does the prefab have the audiosource enabled? Is the AudioSource attached to the same object the script is attached to (i.e. not a child of the object the script is attached to)?

Try modifying your Fire() method as follows for testing:

 public void Fire () {
       Debug.Log( "Enabled: " + GetComponent<AudioSource> ().enabled);
      GetComponent<AudioSource> ().clip = FiringSound;
          GetComponent<AudioSource>().Play();
  }

Also, on a side note... Try storing a reference to your AudioSource in a variable. In most cases you should only be using GetComponent on any given component once (when you know you're going to use it more than once).

It's more expensive to use GetComponent every time you reference a component than to store it in a variable and do it that way.

Comment
Add comment · Show 1 · 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 TheDogePwner · Oct 02, 2015 at 12:34 AM 0
Share

The Prefab, before and after instantiation, has an AudioSource. Also, before playing the sound, it says Enabled: true, and says Enabled: true after

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

8 People are following this question.

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

Related Questions

Sound plays right at the beginning 2 Answers

How can I play audio clips depending on the players movement 0 Answers

Multiple Cars not working 1 Answer

Playing each channel of a midi track on separate audio sources? 0 Answers

Audio & SpaceShip problems 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