- Home /
Sound stop when entering trigger
I have a sound playing from an object when gameplay starts, but I want the sound to stop when I enter the box collider (trigger) added to that object. Can anyone help me?
var Sound:AudioClip;
function OnTriggerEnter ()
{
audio.Stop(Sound);
}
`
that kind of thing must work but the playing sound start with this script or another ?
Answer by Lovrenc · Jan 14, 2013 at 12:38 PM
Is your collision actually happening? Do this:
function OnTriggerEnter (collision : Collision)
{
Debug.Log("Collision! Stop playing audio.");
audio.Stop(Sound);
}
So check if collision happens. If not search for proper collision settings.
Answer by TheHoboCoder · Jan 14, 2013 at 06:36 PM
Are you sure this script is attached to same object where that AudioSource is? Because if not, then that is the problem.
If it is, then it should work. As Lovrenc mentioned, you could try if Debug.Log gives you any feedback.
Answer by JoBrindley · Jan 14, 2013 at 07:03 PM
Yer, the sound audio source is on the same object as the script What do you mean when you say Debug.Log?? I'm really new to all this stuff
Use CO$$anonymous$$$$anonymous$$ENTS to comment!
I wrote the Debug.Log code below....
oh yer, sorry. I've done that before :/ wasn't thinking. and yer, I tried that and it didn't work either.
Its not ment to work. It shows you if you get a collision. Read some unity docs. And since you didnt get any message it means you didnt set up your player/objects correctly.
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
ah, that would be why. Feel kinda stupid now. I've got a uni hand-in in 2 days, so will be properly trying to understand scripting etc after that. Thank you though :)