- Home /
Making a sound play once on collision, can you fix my script?
Hello Unity Answers, I have collected some script from the internet and have tried to put it together. Unfortunately, I am new to coding and I don't even know how to lay it out properly. Would anybody care to fix it for me? It plays a sound when I enter an area, but I have broken it trying to make play once (so if you go back in said area it doesn't play again). Thanks!
pragma strict
var scareSound:AudioClip; var hasPlayed = false;
function OnTriggerEnter(o:Collider){
audio.PlayOneShot(scareSound);
if(!hasPlayed){hasPlayed = true;
}
Answer by lancer · Aug 03, 2013 at 01:00 AM
Try this:
Add a AudioSource to the object with the script attached and uncheck play on awake and select your clip as the audio clip, then add this script:
#Pragma strict
function OnTriggerEnter(o : Collider){
AudioSource.Play();
Destroy (this);
}
@lancer - this will play every time he triggers. I think he wants it to only trigger once and then not trigger in the future.
Answer by ExpiredIndexCard · Aug 03, 2013 at 05:43 AM
Delete the audio source or if you don't need that trigger anymore, delete the whole gameobject. Google how to do these.
Do it your self. You won't learn if pEople just give you answers. Experiment.
Your answer
Follow this Question
Related Questions
Collider + multiple sounds script 1 Answer
Change Audio source on collision 1 Answer
Ignore collision at high velocity. 1 Answer
Trouble with Physics.IgnoreCollision 0 Answers
Unwanted jittery behavior 2 Answers