- Home /
Playing audio loop in ongoing loop which can change over time?
I have an ongoing loop in my Update function which is subtracting the currentStamina variable every second when the player is running.
When it has reached less than 50 it's calling the function ExhaustedAudio. At the moment its playing an oneshot audio, but I want to have a audio looping in there while the currentStamina is under 50 and stop looping when it's above the 50. It's getting harder because the currentStamina will also increase over time when the player is not running. Can anyone give me an example on how to handle this?
var playerExhausted : boolean = false;
Update function() {
if(currentStamina < 50) {
ExhaustedAudio();
}
}
function ExhaustedAudio() {
if(playerExhausted == false) {
Camera.main.audio.PlayOneShot(audioFile);
playerExhausted = true;
}
}
Answer by roojerry · Mar 21, 2013 at 01:59 PM
I would try setting your AudioSource to loop, then use Play() instead of PlayOneShot(). When stamina gets back above 50, just set the AudioSource not to loop and the audio should stop after the end of the last play of your audio file.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094824im_/https://answers.unity.com/themes/thub/images/avi.jpg)