- Home /
How to make the trigger work only once. (SOUND)
I know there are many questions on the topic but none fit my interests. So, I'm working on a horror game, and I am a real begginer but with some knowledge. So I have my trigger and this jumpscare script: #pragma strict
var Alien_Creature : GameObject;
function Start () {
Alien_Creature.SetActive(false);
}
function OnTriggerEnter () {
Alien_Creature.SetActive (true);
}
function OnTriggerExit ()
{
Alien_Creature.SetActive(false); Destroy(Alien_Creature); }
Everytime I walk through the trigger, the SOUND starts playing. I am not sure if it's the jumpscare script's fault or the sound script's fault but here is the sound script: function OnTriggerEnter(other: Collider){
if (!audio.isPlaying){
audio.Play(); Destroy.audio(); } } Help Please :)
Answer by Dblfstr · Feb 25, 2014 at 04:21 PM
Use a bool "hasPlayed" to see if the audio has played once.
var hasPlayed = false;
function OnTriggerEnter(other: Collider){
if (!audio.isPlaying){
if(!hasPlayed){
audio.Play();
hasPlayed = true;
}
}
}
I told you I'm kinda of a newbie, where do I need to place this? $$anonymous$$ake a new script, add it at the beggining of the sound script or...?
Also, if the sound script is attached to the trigger that plays the sound. And you only want to play the sound once. You could always disable the trigger once it been used.
this.setActive = false; after you audio has played.
It tells me to insert a semi-colon at the end (1,5) I see it already has.
I'm not sure if I got the javascript syntax right on declaring a bool variable. I use C#.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I want my trigger sound only to play once! 0 Answers
Play sound on trigger, sound is coming from the trigger 1 Answer
Sound script problem. 0 Answers
Playing sound on collide with a trigger. 2 Answers