I have a door script, when the door is opened from closed I call: PlayOneShot(openFromClosedSound);
I have another method, that stops the source, switched the clips, and then plays it again, for when the door is opening/closing.
I had thought that calling Stop() did not effect sounds played with one shot, I thought they were play and forget, but when I call this method right after calling PlayOneShot, it does not play the one shot sound.
When I comment out my method, the sound is played as expected.
void PlayClip(AudioClip clip) {
// abort if no source
if (audioSource == null) return;
// stop and set new clip
audioSource.Stop();
audioSource.clip = clip;
// set random pitch if fluctuation is set
if (globalPitchFluctuation != 0) {
float currentPitch = audioSource.pitch;
audioSource.pitch = Random.Range(currentPitch - globalPitchFluctuation, currentPitch + globalPitchFluctuation);
}
audioSource.Play();
}
So PlayOneShot does have this “fire-and-forget” quality, but still binds the instance of the AudioSource to the playback of the sounds it started. Calling AudioSource.Stop() would effectively stop all the sounds it started, whether by PlayOneShot or Play for the attached AudioClip.
I think your best shot would be to have a separate AudioSource for your one-shots. If you’re not spatializing these sounds, you could probably use the same source for all of them.
Thanks for the reply and the info! Does PlayOneShot follow the audio source as well? Like if the audio source is moving, and PlayOneShot is called, does the audio get played only from that point, or follow the source, since it binds itself to it?