Trigger sound

  var soundClip : AudioClip;
         
         
         
        function OnTriggerEnter (collision : Collision) {
         
         
         
            audio.clip = soundClip;
         
            audio.Play();
         
        }
       
        function OnCollisionExit (collision : Collision) {
       
        audio.Stop(soundClip);
       
        }

Why is this not working?

Errors:

Script error: OnTriggerEnter
This message parameter has to be of type: Collider
The message will be ignored.

Assets/Piano.js(17,15): BCE0017: The best overload for the method ‘UnityEngine.AudioSource.Stop()’ is not compatible with the argument list ‘(UnityEngine.AudioClip)’.

For a trigger to activate, one of the objects has to have a rigidbody component added to it. Is that the issue?

I think that the script is wrong :confused:

I have never used a collider, but the error message is telling you that it is expecting the “collider” type instead of the “collision” type for your OnTriggerEnter function.

See the scripting reference for OnTriggerEnter
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

audio.Stop doesn’t need you to tell it what clip to stop playing. It knows what clip it’s playing.

I have not tested this at all, but your code should look more like:

var soundClip : AudioClip;

function OnTriggerEnter (other : Collider) {

            audio.clip = soundClip;

            audio.Play();

}

       

function OnCollisionExit (collision : Collision) {

        audio.Stop();

}

This should fix it

 var soundClip : AudioClip;
        function OnTriggerEnter (collision : Collider) {
            audio.clip = soundClip;
            audio.Play();
        }
        function OnCollisionExit (collision : Collider) {
        audio.Stop();
        }
var soundClip : AudioClip;

function OnCollisionEnter (collision : Collision) {

audio.clip = soundClip;

audio.Play();

}

function OnCollisionExit (collision : Collision) {

audio.Stop();

}

This is what I wanted, it took me some time to find out the bug, but I did!