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

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

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.

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.

I’ve got an additional contribution for anyone who has checked all of the above and still can’t figure out what’s wrong.

I had a similar issue for some time in my current project; worst of all, all of the allegedly infringing AudioSources were enabled, consistently, and even attempting to set the AudioSource to enabled before playing it did nothing. On top of everything else, the sounds seemed to play just fine; they just spammed this warning about fifty times on scene start.

After a morning spent in close inspection, I realized that all of my prefabs were referencing audio components in my project tree, not my scene hierarchy. Setting the fields to an audio source on the current prefab (as opposed to, in my case, an audio source attached to the prefab they were based off of) fixed everything–no more warnings.

When all else fails, check your references. Clicking on the Audio Source field should highlight what it’s referencing. If it’s something like “Project->Black Book” instead of “Scene->Red Book”, then that’s your problem.

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.

Make sure you have checked the AudioSource Component.I just met the same mistake and I found I fogot to check the AudioSource Component.

it happens because you forgot about second script destroying the object-owner of sound

I have the same problem too! This is my code:

public float projectileSpeed;
private Rigidbody2D rb;
public GameObject particle;
[SerializeField] private float dmg = 0;
[SerializeField] private AudioSource explode;
// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = transform.right * projectileSpeed;
}

// Update is called once per frame
void Update()
{
    
}
private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "enemy")
    {
        collision.gameObject.GetComponent<enemyHealth>().hurt(dmg);
        playAudio();
        Instantiate(particle, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}
private void OnCollisionEnter2D(Collision2D collision)
{
    playAudio();
    Instantiate(particle, transform.position, transform.rotation);
    Destroy(gameObject);
}
void playAudio()
{
    explode.Play();
}

Hola, yo tuve el mismo problema y aunque las respuestas anteriores eran muy interesantes, la verdad no tenía tanto tiempo para probar, así que revisé las jerarquías de las acciones y añadí unos 5 segundos adicionales para que el objeto no de destruyera de inmediato y así lo resolví:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fruta : MonoBehaviour
{
private AudioSource audioSource;

[SerializeField] private int cantidadPuntos;


void Start()
{
    audioSource = GetComponent<AudioSource>();
}



private void OnTriggerEnter(Collider other)
{
    if(other.CompareTag("Player"))
    {
        audioSource.Play();
        ControladorPuntos.Instance.SumarPuntos(cantidadPuntos);
        Debug.Log("Come todas las moras que puedas");
        Destroy(gameObject, .5f); 
    }

}

}