Audio question

I tried to search button and it gives me a 504 error… I’ve googled this, and checked multiple pages but I’m just not understanding it.
I have an enemy object, that when its health is at or below 0, it blows up, and creates an explosion prefab that plays an explosion sound… that works fine. I attached an audio file called “ricochet” to the enemy object thru the variable

var soundRicochet : AudioClip;

So. I then have the enemy have health, and each bullet lowers it’s health, and if it’s health is not zero or less, it’s supposed to play the ricochet sound. It doesn’t play the audio file, and gives me a “there is no audio source attached to the object…” I’ve read multiple things on google, and people are saying stuff like you gotta put

@script RequireComponent (AudioSource)

I try it but maybe I’m putting it in the wrong spot. Here’s my code.

var soundRicochet : AudioClip;
.
.
.
function OnTriggerEnter( otherObject: Collider) // collision with bullet, lowers health
{
	if (otherObject.gameObject.tag == "bullet")
	{  
		playerScript.playerScore += 100;	
		enemyHealth -= 1;
		if (enemyHealth >= 1)
		{
		audio.PlayOneShot(soundRicochet);
		}
	}
}

so as you can see, if the enemy still has health after being hit, it should play the ricochet sound so you know it hit the enemy. What am I doing wrong? Thanks in advance!
EDIT: Also, I have dragged and dropped the ricochet.wav from the project tab to the “soundRicochet” spot in the inspector area.

Did you attach an Audio Source component to that GameObject?
Click the object with that script, go up to Component, Audio, Audio Source.

I didn’t know I had to actually have that part attached through Component > Audio > Audio Source… It works now! I assumed that me dragging it to the slot provided by the var in the inspector worked… but I just read that that is only there for a shortcut, and doesn’t really do much anyways. I appreciate your help! :slight_smile:

It’s also handy in the case of explosions etc, attach an Audio Source to the explosion prefab/particle then drag the sound directly on the audio source and enable “Play On Awake”, so it plays that sound as the explosion prefab is instantiated. Bit less coding.