Second AudioClip won't play
I just started game developing and am making a 2D space shooter. I'm having it so when you shoot an enemy ship, it'll play a hurt noise until it dies after 3 hits, and then I want an explosion sound. Right now, the hurt noise is playing, but when the ship gets destroyed, it won't play the explosion sound. I have no idea why it won't work because the code is basically the same for both sounds so why would the first one work but not the second. Any help would be great. Here's the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyController : MonoBehaviour {
public AudioSource audioSource;
public AudioClip explosionClip;
public AudioClip owClip;
private int health = 3;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
audioSource.PlayOneShot(owClip, 1f);
Destroy(collision.gameObject);
health--;
if (health <= 0)
{
KillSelf();
}
}
}
private void KillSelf()
{
audioSource.PlayOneShot (explosionClip, 1f);
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Second AudioClip won't play 0 Answers
Cracking at end of audio? 0 Answers
Need help: new sound isnt played correctly anymore 2 Answers
Some sounds won't play 0 Answers
Audio is way too soft on mobile but alright in Unity Editor! 0 Answers