audio.PlayOneShot() + Update() = mess?

Hi.

I’ve been having trouble getting audio.PlayOneShot() to play only once, and not overlap with the same sound each frame, cause Update() is getting called each frame. The thing I want to do is to play a sound once a boolean is true.

Things I’ve tried:

  1. I created a new boolean variable, and deactivated it after the audio.PlayOneShot() line (if the boolean is true, play a sound, after that line, deactivate it)
  2. I created a boolean function, and play the sound after the “return true;” line
  3. Call it in another (regular void) function (same result, the Update() function is still calling the newly created function each frame),
  4. I have also tried to Invoke() that function.

How could I play a sound when the boolean mentioned in the beginning is turned true?

C# version:

bool playAudio=false;
void Update()
{
    if(playAudio)
    {
        audio.PlayOneShot();
        playAudio=false;
    }
}

Javascript version:

var playAudio = false;
function Update()
{
    if(playAudio)
    {
        audio.PlayOneShot();
        playAudio=false;
    }
}

Just add the script that isn’t in your file to the appropriate areas.