When I try to play an audio clip to signify an object being destroyed I get the error:
Can not play a disabled audio source
It seems like when you destroy an object you can’t play an associated clip but it’s not clear from research on the Internet how to adapt my code. Code shortened to relevant bits.
public class Targets : MonoBehaviour
{
//Sound effects
public AudioClip targetDestroyedSound;
public AudioSource gameSounds;
// Start is called before the first frame update
void Start()
{
gameSounds = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
private void OnMouseDown()
{
// Function allows users to click on targets and destroy them
if (gameManager.isGameActive)
{
// Destroyed target game sound
gameSounds.PlayOneShot(targetDestroyedSound, 1.0f);
// create particle explosion when clicked on
Instantiate(particleExplosion, transform.position, transform.rotation);
// UpdateScore taken from Game Manager class
gameManager.UpdateScore(pointValue);
// destroys game object if clicked on
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
if (!gameObject.CompareTag("Bad"))
{
gameManager.GameOver();
}
}