- Home /
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();
}
How about
GetComponent<AudioSource> ().PlayOneShot( FiringSound );
"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 ;
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.
Are you sure the component is enabled in the inspector ?
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.
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);
}
While its unclear if this helped the OP, This was defiantly an eye opener for me Thank you my dood!
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?
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
The Player has an Audio Listener. Like I said, it works on Awake.
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.
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
Follow this Question
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