So I made a game where you shoot a laser and if it hits the enemy it makes an explosion sound and destroys the enemy. The only thing is whenever I try to get the audio to play it doesn't which I assume is caused by the enemy being destroyed. I have tried to use the Play() methods and PlayOneShot() methods but they both don't seem to help me out.
This is my current code:
public AudioClip explosion;
public AudioSource explosionAudioSource;
public void OnTriggerEnter2D(Collider2D other)
{
void Start() {
explosionAudioSource = GetComponent<AudioSource>();
}
if (this.tag == "Enemy" && other.tag == "Laser")
{
explosionAudioSource.PlayOneShot(explosion);
Destroy(other.gameObject);
Instantiate(enemyExplosion, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
Destroy(this.gameObject);
uiManager.UpdateScore();
}
Thanks!