- Home /
Audio.PlayOneShot problem
Hello everyone I am working on a project, simple fps. I have a zombie enemy roaming and I want it to PlayOneShot when I am detected, the problem is when I get close and it detects me the audio plays in stacks .. like 50 times together if you know what I mean.. any suggestions ? thank you
public AudioClip Dtct;
void Update () {
if(Vector3.Distance(player.position, transform.position)<distance){
audio.PlayOneShot(Dtct);
}
}
Answer by Orlando Bascunan · Jul 28, 2013 at 03:03 PM
Hola! You should make a bool storing if its already played like this
public AudioClip Dtct;
private bool soundPlayed = false;
void Update () {
if(!soundPlayed){
if(Vector3.Distance(player.position, transform.position)<distance){
audio.PlayOneShot(Dtct);
soundPlayed = true;
}
}
}
Answer by dan19 · Jul 28, 2013 at 02:55 PM
This happens because once you approach the zombie, you start calling PlayOneShot()
every frame, and it starts playing the sound many times a second. You should somehow prevent subsequent calls. Try this:
if(Vector3.Distance(...) < distance && !audio.isPlaying) {
audio.PlayOneShot(Dtct);
}
Thank you for the quick reply, but this will not work since it is checking if audio is playing, so once it does it plays the audio and the same problem happens x[
It will start playing again only when the previous sound stops playing. Did you notice the boolean negation (!) ?
But it will start playing again once it is done if you stay near the zombie. If you don't want that' you'll have to use some additional state to remember that you don't want to play the sound any more. Like this, for instance:
private bool nearZombie = false;
public void Update() {
if(Vector3.Distance(...) < distance) {
if(!nearZombie && !audio.isPlaying) {
audio.PlayOneShot(...);
}
nearZombie = true;
} else {
nearZombie = false;
}
}
Although I haven't checked whether or not isPlaying is affected by PlayOneShot(). It is definitely affected by Play() though.
I appreciate your effort, the first code you wrote gave me the same problem I was having. I used Orlando's advice and it got it fixed, which is similar to the code you wrote just now, thank you very much again :))
Yep, this page says that isPlaying is not affected by PlayOneShot(). So you should replace audio.PlayOneShot(...)
with something like
audio.clip = Dtct;
audio.Play();
Answer by LaurynasLubys · Apr 04, 2018 at 11:09 AM
I experienced this problem as well, but I understood it as if I have too many SoundSources on objects (total amount of sound sources in the scene).
My setup was that I had a new sound source per sound clip. For example object would have 5 sound clips, therefore script would create 5 sound sources and parent to the object.
Instead, I modified my script to switch sound clips on random to the same sound source. So that one object would always have a single sound source. Perhaps it is intended like that in a first place. Anyway, now all sounds are played as they should, and I don't get this error. One problem with that is that code needs to reassign sound clip to the sound source each time when new sound should play.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111045im_/https://answers.unity.com/themes/thub/images/avi.jpg)