How to load audio clips from a folder to the script?

hello i’m a noob and i’ve been trying to load Audio clips to my script from a folder called Sfx on my Assets folder using this method

public class soundManagerScript : MonoBehaviour
{
    public static AudioClip killEnemy, death;
    static AudioSource AudioSrc;
    
    // Start is called before the first frame update
    void Start()
    {
        killEnemy = Sfx.Load<AudioClip>("select2");
        death = Sfx.Load<AudioClip>("gameover");
    }

but i get an error on Sfx how should i call it? is there an another method?
also i tried to put the audioclip killenemy on the enemy prefab but since the enemy get destroyed when killed the clip doesn’t play

I suppose you read somewhere about Resources.Load<AudioClip>("filename"); to load audio files from the Resources folder and tried to do the same with Sfx folder.

The Resources folder is a special case in Unity. You cannot rename the folder, but you can nest it. If you put your files in Resources/Sfx/, you will be able to use Resources.Load<AudioClip>("Sfx/select2"); and Resources.Load<AudioClip>("Sfx/gameover");.

You can find more info here:

I don’t know how to call from a folder, I’m not that experienced. What I tend to do with audio clips is just create empty GameObjects and add an AudioSource component to them. Then simply call the Play() function on that object when you want the noise.

so if you have an enemy noise for example, you would do this…

Public GameObject enemyNoise;
Public AudioSource enemySound;

Void Start()
{
enemySound = enemyNoise.GetComponent();
}

Void MakeKillNoise()
{
enemySound.Play();
}

Theres more to it than this obviously but this is the basics for make a noise when you want to, like you said with the prefab it destroys itself as its making the sound. This way you can make the noise whenever you want. Just remember to turn off Play When Awake and Loop on the component.

public class Sound : MonoBehaviour
{
    [SerializeField] private AudioSource audioSource;
    [SerializeField] private AudioClip clip;
    [SerializeField] private float volume = 0.5f;

    void Start()
    {
        audioSource.PlayOneShot(clip, volume); // volume is not necessary
    }
}

I personally would create a Sound Manager gameObject with an audioSource and an audioScript attached to it. This is an example of how you could write the script.

I also would not try to reference any folders, I would just drag and drop the audio Sound in the serialized variable in the script on the gameObject.

If you dont know how to assign variables within the inspector:

(just drag and drop or click on the round icon next to the variable and you can choose a file there)

More on Sound in this article: