- Home /
How to play sound when player walks through box collider trigger?
Ok so I want it to play a door creak sound when you open the door so I made a script and attached it to the same box collider (trigger is on) that opens the door but my script isn't working. Heres the script
function OnTriggerEnter(other: Collider){
if (!audio.isPlaying){
audio.Play(); Destroy.audio(); ) }
Thanks!
What isn't working about it? it it a script error or?
It says add a semi colon to the end which I did but it still wont work. Where should I put it?
There's a few things I see wrong here - You are destroying something, probably the audio source, instantly after getting it to play. Instantly wouldn't really describe it, it basically doesn't even play. You should pass the destroy function the time parameter if you want it to wait a little bit so it doesn't cut off the audio.
The next thing is Destroy is called as a method/function, and does not have any variables that you can access, so Destroy.anything will not work. It's used like this:
Destroy(object, time_delay);
Where 'object' can be a component/game object/etc, time delay is self explanatory. So in your case, Destroy(audio)
delete the Destroy.audio(); ) that would destroy the AudioSource component before it has a chance to play
Answer by RyanPaterson · Jan 04, 2014 at 11:56 PM
I'm not sure what you what "isn't working" about it, like script or just can't hear the sound, and I don't use javascript so I can't adjust your script, but you did have a ')' instead of a '}'
and i'm thinking maybe you don't want to destroy() right after it, i haven't tried it myself but it's likely that it'll destroy the sound quicker than it would play. Just something to maybe look into.
function OnTriggerEnter(other: Collider){
if (!audio.isPlaying){
audio.Play();
Destroy.audio();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move from point A to point B, then destroy? ( java S) 2 Answers
Programming noob help 1 Answer
Always play music? 1 Answer
Make the first person controller transparent? 1 Answer
Instantiate cloned prefab to local position of an empty object 1 Answer