I have no idea how to solve this. The goal is that i want a hitsound to play when a bullet hits its target and when target has 0 health it explodes and plays deathSound:
public AudioClip hitSound;
public AudioClip deathSound;
private AudioSource source;
private float volLowRange = 0.5f;
private float volHighRange = 1.0f;
SpriteRenderer spriteRend;
void Start() {
source = GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Bullet" && gameObject.tag != "Bullet" && gameObject.tag != "BigBullet") {
health--;
float vol = Random.Range(volLowRange, volHighRange);
source.PlayOneShot(hitSound,vol);
}
}
void Update() {
if (health <= 0 && gameObject.tag == "Player" | gameObject.tag == "Enemy") {
Instantiate(tankExplosionPrefab, transform.position, transform.rotation);
Die ();
}
if (health <= 0 && gameObject.tag == "Player" | gameObject.tag == "Enemy") {
float vol = Random.Range(volLowRange, volHighRange);
source.PlayOneShot(deathSound,vol);
Die ();
}
The hitSound plays fine, but when the deathSound is about to play I get the following error:
Can not play a disabled audio source
UnityEngine.AudioSource:PlayOneShot(AudioClip, Single)
Why is this??