- Home /
Question by
swooshgames · Aug 18, 2020 at 01:19 AM ·
triggerfps
Is it possible to stop calling a function in the Update function once it is called?
I am trying to get the sound of footsteps on grass to play when the user is doing something that triggers it, but for that i need to call its function GrassRunSound()
in the Update()
function. But that means when the sound should play, it plays 70 times per second (or whatever my current fps is at). So is there anyway i can stop the GrassRunSound()
being called every frame once it's triggered?
I don't know if it helps but here's the variable and the function:
public AudioSource grassRun;
void GrassRunSound()
{
if (isGrounded && speed == 3f)
{
grassRun.Play();
grassRun.loop = true;
}
if (speed <= 3)
{
grassRun.Stop();
}
if (isGrounded == false)
{
grassRun.Stop();
}
}
PS: I'm fairly new to coding so i know all the if statements might be a little "noob-ish"
Comment
Best Answer
Answer by SoNiice2 · Aug 18, 2020 at 02:57 AM
kinda easy workaround:
private bool playedSound = false;
void Update() {
if (!playedSound)
GrassRunSound();
}
void GrassRunSound() {
playedSound = true;
[..]
}
set the variable to false again after x seconds (sound duration obv)
Your answer
![](https://koobas.hobune.stream/wayback/20220613005906im_/https://answers.unity.com/themes/thub/images/avi.jpg)