Projectile Sound Bug with Solution

Right now the projectile is generating SFX, but upon impact, when the gameObject is destroyed, the sound source also gets destroyed.

You can solve this by not destroying the projectile gameobject but rather, delaying it by a few seconds.

I’ve added a foreach loop to destroy 2 objects & the gameobject prefab:

  1. for the object to destroy upon impact
  2. to destroy the particle effect that gets destroyed slightly later after impact
  3. destroy the entire gameobject after a few seconds.

Here’s the code snippet: (add serialize field for the gameObject to destroyAfterHit)

            foreach (GameObject toDestroy in destroyOnHit)
            {
                Destroy(toDestroy);
            }

            foreach (GameObject toDestroy in destroyAfterHit)
            {
                Destroy(toDestroy, destroyAfterHitTime);
            }
            
            Destroy(gameObject, lifeAfterImpact);  

Yes, that will work good job. Other alternative solutions:

a) Use something like AudioSource.PlayClipAtPoint
b) Have a dedicated sound system, separated from the source GameObject, so that your sound doesn’t depend an the state or existence of the source GameObject (advanced)

well done

Privacy & Terms