- Home /
Audio won't play on collision
I've been working on some code to allow the player to collect a health power up, and then destroy the power up and play an audio clip signifying the player has picked it up. However when the power up is destroyed the audio does not play. If I remove the code to destroy the item, the audio plays fine, however, obviously, the item does not disappear. I'm not sure why destroying the item should affect the audio since the audio is attached to the player not the item itself.
I've also tried hiding the object and waiting until the audio clip is complete before destroying it, however this gave inconsistent results. Sometimes the clip played as it should and other times it didn't, and still other times it played multiple times before being destroyed, so I went back to the original method.
I've posted my CollisionDetection class below:
public class CollisionDetection : MonoBehaviour {
private AudioSource m_AudioSource;
public AudioClip m_CollectSound;
void Start () {
m_AudioSource = GetComponent<AudioSource>();
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "healthItem") {
print ("Collide");
m_AudioSource.PlayOneShot(m_CollectSound);
Destroy (hit.gameObject);
}
}
}
Any insight into why this would be happening would be greatly appreciated. I'm nearly tearing my hair out over here trying to get this to work.