Problems getting onHit audio to play on collision

I’m working on the “Sound Effects and UnityEvents” lecture.

My audio clip won’t play when the fireball or arrow strikes the target.

I have successfully gotten all other sound effects to work, but I’m not sure what the issue is here.

  • I have followed the code from the lecture (see below).
  • I have created an audio source for the onHit event.
  • I have hooked up the onHit event with the Audio Source gameobject and selected “AudioSource.Play” from the drop down menu.
  • And, yep, the collider is present and set to “is trigger”
  • To debug, I created a log message “hit audio triggered” that is displayed in onHit event. That actually worked, so I know “onHit” is being called.
  • I’ve recreated the gameobjects from scratch
  • I’ve swapped out audio clips to see if it was the clip that was the problem. The clips work in any other context but not in onHit.
  • I’ve mostly tested the fireball, but arrows sound effects don’t work onHit either.

Not quite sure what’s going on with this. I’d love any ideas.

Here’s my projectile code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Attributes;
using UnityEngine.Events;

namespace RPG.Combat
    public class Projectile : MonoBehaviour
        [SerializeField] float speed = 1f;
        [SerializeField] bool isHoming = false;
        [SerializeField] GameObject impactFX;
        [SerializeField] float impactFXTime = 2f;
        [SerializeField] float maxLifetime = 10f;
        [SerializeField] UnityEvent onHit;

        Health target;
        GameObject instigator = null;
        float damage = 0;

        private void Start()

        void Update()
            if (target == null) return;

            if (isHoming && !target.IsDead())
            transform.Translate(Vector3.forward * speed * Time.deltaTime);

        public void SetTarget(Health target, GameObject instigator, float damage)
   = target;
            this.damage = damage;
            this.instigator = instigator;

            Destroy(gameObject, maxLifetime);

        private void LookAtTarget()

        private Vector3 GetAimLocation()
            CapsuleCollider targetCapsule = target.GetComponent<CapsuleCollider>();
            if (targetCapsule == null)
                return target.transform.position;
            return target.transform.position + Vector3.up * targetCapsule.height / 2;

        private void OnTriggerEnter(Collider other)
            if (other.GetComponent<Health>() != target) return;
            if (target.IsDead()) return;
            target.TakeDamage(instigator, damage);

            speed = 0;


            if (impactFX != null)
                GameObject newImpactFX = Instantiate(impactFX, GetAimLocation(),
                Destroy(newImpactFX, impactFXTime);


The issue is that the gameobject that the AudioSource is attached to is destroyed immediately after it sounds. Check out lecture 164 “Destroy The Unwanted”, and it should give you an idea how to keep the audio effect around after the impact.

That makes sense! Wow! Thanks, Brian. I thought I would challenge myself to destroy the gameobject on my own with my own code, but now I can see the reasoning behind the way it was handled in the lecture.

Thanks again!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms