- Home /
Can not play a disabled audio source. Why randomly?
Hi,
I randomly started getting the error, but I have not changed anything within the codes. The sounds were working but it confusing as to why this would randomly happen.
I have set the variables as so:
public AudioSource JumpSound;
public AudioSource DieSound;
public AudioSource FalldownSound;
and here is how I play them:
void OnCollisionEnter2D(Collision2D other)
{
if (!hasPlayed)
{
DieSound.Play();
hasPlayed = true;
}
if (!hasPlayed2)
{
FalldownSound.Play();
hasPlayed2 = true;
}
Die ();
}
What is the problem? I don't see why this will stop working randomly. I have checked all my audios and they're all ticked in the inspector.
EDIT:
This is the error message.
Can not play a disabled audio source UnityEngine.AudioSource:Play() UFO:OnCollisionEnter2D(Collision2D) (at Assets/Scripts/UFO.cs:35)
Line 35 is DieSound.Play();
The same error is seen for all 3 audio sounds I have
you'd be better off having in a single AudioSource
and assigning different AudioClip
s to it.
also, when you get an error, try posting the complete error message so that the helpful folks on the forums/answers can get all of the info available and not have to guess at stuff...
help us help you ;)
I added the error message in the original question. I didn't know I can just copy and paste it!
How do I go about have more than audio clip to 1 audio source? The inspector only allows 1 clip?
I wonder how you assign your AudioSources if they are on the same object...
$$anonymous$$aybe try something like this :
public AudioSource source;
public AudioClip[] clips;
void OnCollisionEnter2D(Collision2D other)
{
if (!hasPlayed)
{
source.clip = clips[0]; //DieClip
source.Play();
hasPlayed = true;
}
if (!hasPlayed2)
{
source.clip = clips[1]; //FalldownClip
source.Play();
hasPlayed2 = true;
}
Die ();
}
or if you don't want an array of clips, use:
public AudioClip JumpSound;
public AudioClip DieSound;
public AudioClip FalldownSound;
Try looking in the inspector to make sure the sound is enabled.
Answer by alch_roy · Dec 20, 2015 at 03:08 AM
If you destroy the object before you are trying to play the sound you will get this error. an if(object != null) source.Play(); solved it for me
Answer by DoctorWho · Jul 09, 2014 at 11:54 PM
click this to get right to everything audio
ok, if your audio source is disabled then it will not use the play function, look at your audio source it has a check box for being enabled or not, if its not checked it will not be updated unless you enable it with code. also inside it is a play on awake, this is what the code your using is acting on, and it to has a check box. there is a video all about sound made by a unity programmer in the online training archive hers that link : click : read or watch or both.
Answer by Meet Jon Black · Jul 21, 2015 at 03:39 PM
If the issue does not have anything to do with the audio source being enabled, then you probably just need to Instantiate the AudioSource variables(JumpSound, DieSound and FalldownSound). If it is not in the actual game hierarchy, then theoretically it isn't enabled and it is just a file on your computer. I hope this helps anybody else with the same error, because it sure helped me.
Answer by Drudail · Oct 15, 2017 at 05:57 PM
I had a similar issue, which was thay the object was being destroyed at the same time as the audiosource was supposed to play. I created a SoundController GameObject, unloaded all sounds to it and in script gave it the public AudioSource variables. In my GameController script I instantiated the SoundController and used the clips from there. Now whenever the object is destroyed the sound comes from a still-instantiated object so the sounds play. Bonus, it cleaned up a lot of other objects by unifying the location of all sound objects.
To make sure they play, the destroying script is also in GameManager and each object that hits passes in a strong to determine which sound to play.
Answer by hjzhang · Sep 06, 2016 at 10:32 AM
Make sure you have checked the AudioSource Component.I just met the same mistake and I found I fogot to check the AudioSource Component.
Hello! I have had the same problem, although I am making a different kind of game and have different code for the audio. Please help!! I have my publics and privates set to:
public AudioClip sound;
private Button button { get { return GetComponent<Button>(); } }
private AudioSource source { get { return GetComponent<AudioSource>(); } }
and my code is:
void Start()
{
gameObject.AddComponent<AudioSource>();
source.clip = sound;
source.playOnAwake = false;
button.onClick.AddListener(() => PlaySound());
}
void PlaySound()
{
source.PlayOneShot(sound);
}
}
I have my audio in the spot where unity asks for it, and yet it gives me the same error message. I have no problem playing the background audio, however.
Please help!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613153607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to start a sound not from it's start ? 2 Answers
Read audio/music currently playing on speakers? 0 Answers
[Rhytm game] How to spawn on exact time of music? 3 Answers
Audio is always playing 0 Answers
How to avoid audio glitches whilst switching volume of an Audiosource each Update()-Frame? 0 Answers