Trigger Audio help?

I am trying to get my trigger(proximity) to play a sound when I enter a spherical trigger. I have the code written below. But it does not work. If anyone could give me a pointer that would be very helpful. Thank you.

var sound1 : AudioClip; 
 

function OnTriggerEnter () 
 
  { 
      audio.clip = sound1; 
      audio.Play (); 
  } 
  }

Humbly yours,

Gary

Try this code:

var sound1 : AudioClip;

function OnTriggerEnter (){

sound1.Play ();
}

You’ve already got sound1 assigned in the editor I assume, so all you have to do is call sound1.Play() to start it. There is no need to create a new clip from the clip. If you did for some reason want to do that, the code should be:

var sound1 : AudioClip;

function OnTriggerEnter ()

{
var audio : AudioClip = sound1;
audio.Play ();
}

This is untested, but from what I remember from doing a few sound bites, this should work.

Jeff

Error using script. Could someone help me refine this further? I restarted Unity as well to see if that would help.

Assets/Standard Assets/Scripts/Trigger_audioClip.js(5) error BCE0019: ‘Play’ is not a member of ‘UnityEngine.AudioClip’.

var sound1 : AudioClip; 

function OnTriggerEnter (){ 

sound1.Play (); 
}

Thanks for your help so far.

Gary

You assign the audioclip to an audiosource, and then use audiosource.Play.

audio.clip = sound1;
audio.Play();

Starmanta thanks. Where would I insert your code into the code I have above? Thanks again.

Gary

It’s the body of your OnTriggerEnter function.

Hi everybody !
Sorry to put this old topic up but,
I search a little solution …
I put 2 empty gameObject in my scene :
First : got a OnTriggerEnter audio.play
and the Second : got a audio.stop
If you didn’t understand (^^),
I would like the second stop playing the first
I try,I try many different ways,but nothing !

Please,Can you help me ?

:sweat_smile:

Not sure if this will work properly

var sound1 : AudioClip;
var sound2 : AudioClip;

function OnTriggerEnter()
{
  audio.PlayOneShot(sound1);
}

function OnTriggerExit()
{
  audio.PlayOneShot(Sound2);
}

Not sure if that’ exactly how you want it done but try playing with it see what happens.

Ray

Thank you for your reply !
there is no problem anymore.
Thanks for your attention

Gul