Audio only playing sometimes

Hi,

I’m trying to get an audio clip playing when something happens to an object but am having trouble. I have the clip attached to the game object and in its behaviour it calls

public void ApplyDamage(float damage)
{
    Debug.Log(string.Format("{0} damage applied.", damage));
	audio.Play();
	HitPoints -= damage;
    if (HitPoints <= 0)
    {
        Destroy(gameObject);
        Instantiate(JointBreakEffect, transform.position, Quaternion.identity);
    }
}

I want this clip to play only when the object’s hit points go below zero (i.e. its destroyed). The clip will play fine in its current location but if I move it inside that if statement it won’t play. Anyone have any clue why?

Thanks in advance

Answering my own question but found a solution if I use

public void ApplyDamage(float damage)
{
  Debug.Log(string.Format("{0} damage applied.", damage));
  HitPoints -= damage;
  if (HitPoints <= 0)
  {
    AudioSource.PlayClipAtPoint(audio.clip, transform.position);
    Destroy(gameObject);
    Instantiate(JointBreakEffect, transform.position, Quaternion.identity);
  }
}

Instead it works. I’d still appreciate it if someone could explain why this is the case and I can’t just access it the otherway?

I suspect you are destroying the object AND the audio source, killing the sound at the nest. Try to use Play() with a delayed Destroy to test it (for instance: Destroy(gameObject,2.0f)). Your solution seems to confirm this hypothesis, because PlayClipAtPoint instantiates an audio source for the duration of the sound - so the original audio source is no more needed.