- Home /
Audio PlayOneShot Help
Im trying to create a sprinting script. I have everything working stamina, sprinting speed, but my audio is not working. Im trying to make that when your stamina is less than (amount) it plays the sound once. Im not going to post the whole script but here is the part im stuck in :
if(stamina <= 30 && !audio.isPlaying)
{
audio.PlayOneShot(exhaustion);
}
Answer by LijuDeveloper · Oct 24, 2013 at 07:18 AM
In this code some problem. For example , if player's stamina = 30 , audio will play. whenever stamina = 29,28 ,27 ...........etc ,in each condition audio will play. To avoid this , use a boolean like
public bool hasPlayedAudio = false;
if(stamina <= 30 && !hasPlayedAudio)
{
hasPlayedAudio = true;
audio.PlayOneShot(exhaustion);
}
If once audio play, hasPlayedAudio become true and audio will not play again.
Thanks :) Had to change a few things but now works like dream ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613120533im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I can't make my ball jump using distToGround and Physics.Raycast 0 Answers
Footstep Script (NullReferenceException: Object reference not set to an instance of an object) 1 Answer
Combining 2 Vectors for Diagonal Movement 1 Answer
2 audiolisteners in 1 scene 0 Answers
move a camera to different positions of a sorted array 2 Answers