Making Different Sounds Play on Collision

Hello anybody,

I am trying to play a certain sound on the first collision, and then a different sound on subsequent ones. But, even the basic code below won’t make a sound. Any hints on getting this code working?

Thank you! Any response will be greatly appreciated. The code below runs without errors, but won’t sound.

function OnTriggerEnter() {

var x9:AudioClip;
var x10:AudioClip;
// audio.Play(); // this one works for one sound

// call another function to get the sounds and play them
Playsounds(x9);
// @script RequireComponent(AudioSource) does not work

}
//@script RequireComponent(AudioSource) doesn’t work here either

function Playsounds(myclip:AudioClip) {
audio.PlayOneShot(myclip);
print(“Hello2”); //it’s printing, so I know it has gotten here
//@script.RequireComponent(AudioSource) fugeddaboutit
}

When you’re a newb you’re a newb… the key was declaring the variables outside the function… this works now:

//function for calling one of two sounds, make sure both clips are attached to script in inspector
var p9:AudioClip;
var j9:AudioClip;

function OnTriggerEnter() {

print(“Hello1”);

audio.PlayOneShot(p9); // or select the other clip

}

Have a look in the demo project (angrybods) and how they handle audio.

It’s something like this:

player checks for hits
check if colliding object has a customscript AudioScript attached
grab the sound form the object ( so you add sounds to your objects script…)
play the sound

I am new to Unity too, but it was a solution that interesting