The question is answered, right answer was accepted
How can I make this clip play once
Hello, I would like to make this audio clip only play once. So the first time you enter the trigger is the only time it can work.
using UnityEngine; using System.Collections;
public class SoundDistance : MonoBehaviour { public AudioClip Scream;
void OnTriggerEnter(Collider other)
{
if(other.tag=="Player")
AudioSource.PlayClipAtPoint(Scream, new Vector3(5, 1, 2));
}
}
Answer by EDevJogos · Nov 23, 2017 at 11:56 AM
Just seting a bool to true not enough? like bool playedOnce = false
and then:
if(!playedOnce && other.tag=="Player")
{
playedOnce = true;
AudioSource.PlayClipAtPoint(Scream, new Vector3(5, 1, 2));
}
I just tried that and the audio still plays every time you enter the trigger collision
Then there's something else going on, should work. This Trigger is the only point where you play this audio? The scene is loaded again or something that would somehow set playedOnce
back to false
?
Try putting a Debug.Log("Played once: " + playedOnce);
before the if statement
, because once it enters the if statement for the first time playedOnce
will be seted to true
and should not enter this if
anymore.
It is still happening every time. Here's my script again. have I maybe misplaced something you told me to add in?
using UnityEngine; using System.Collections;
public class SoundDistance : $$anonymous$$onoBehaviour { public AudioClip Scream;
void OnTriggerEnter(Collider other)
{
bool playedOnce = false;
Debug.Log("Played once: " + playedOnce);
if(!playedOnce && other.tag=="Player")
{
playedOnce = true;
AudioSource.PlayClipAtPoint(Scream, new Vector3(5, 1, 2));
}
}
}
Follow this Question
Related Questions
OnTriggerEnter/Exit Rapid Firing? 0 Answers
1 Restart Scene multiple levels. Is this possible? - C# - 1 Answer
Using Multiple Activated Triggers to trigger a Separate Event. 0 Answers
(SOLVED) Need help with Script that is supposed to get components of an AI and change them 1 Answer
Help with a door slam trap 0 Answers