Cant Play Sound On Destroy

hi trying to play a sound on Destroy but it’s not playing I don’t know what I’m doing wrong really and not a lot I can explain

here’s the code maybe you experts can help me, it could just be me not looking at it the write way I don’t know

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Asteroids : MonoBehaviour
{
    public float Torque;
    public Rigidbody2D rb;

    public GameObject asteroidLarge;
    public GameObject asteroidSmall;

    void Start()
    {
        float torque = (Torque);

        rb.AddTorque(torque);

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag ("bullet"))
        {

            Destroy(other.gameObject);
            {

                if (asteroidLarge);

                Destroy(gameObject);

            }
                
        }
    }

}

and here is the code for the SoundManagerScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManagerScript : MonoBehaviour
{

 public static AudioClip PlayerDeathSound, playerBulletSound, enemyDeathSound, AsteroidExplosion01, AsteroidExplosion02, AsteroidExplosion03;
 static AudioSource audioSrc;


    void Start()
    {
        PlayerDeathSound       =    Resources.Load<AudioClip>("PlayerDeath");
        playerBulletSound      =    Resources.Load<AudioClip>("Fire");
        enemyDeathSound        =    Resources.Load<AudioClip>("EnemyDeath");
        AsteroidExplosion01    =    Resources.Load<AudioClip>("Asteroid01");
        AsteroidExplosion02    =    Resources.Load<AudioClip>("Asteroid02");
        

        audioSrc = GetComponent<AudioSource>();
            
    }

   
    void Update()
    {
        
    }

    public static void PlaySound (string clip)
    {
        switch (clip)
        {
            case "Asteroid01":
                audioSrc.PlayOneShot(AsteroidExplosion01);
                break;
        }
    }

}

I’ve been working on this for since 6 am and its 8 pm now and I’m getting tired and I’ve still got to figure out how to play an animation after destroy too, I gave up on that I couldn’t figure it out but 1 thing at a time

Regards
Andy

I am assuming you are destroying the gameobject with your sound manager script attached to it.


Long story short you should probably not be destroying anything in Unity. Destroy deallocates the gameobject and flags it for garbage collection.

Garbage collection causes performance dips so avoiding deallocation during gameplay is generally a good idea.

Try spawn pooling, which should fix your problem if your sound isn’t directional. if it is you could hide.disable it and wait for the sound to finish before returning it to your spawn pool.

void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag (“bullet”))
{
Destroy(other.gameObject);
{
//play the sound here
if (asteroidLarge);//this “;”
{
//play sound here
Destroy(gameObject);
}
}

         }
     }