How do I stop audio?

bool play;
public void Start()
{
play = false;
}
public void GazeEntra()
{
play = true;
this.GetComponent().Play();
this.GetComponent().Play(“cubito”);
if (play == true) {
this.GetComponent().Pause();
this.GetComponent().Play(“quieta”);
}

Hello! I have this object which everytime I gaze at it it will start an animation and an audio clip.
I want the audio to stop and the animation to change once I gaze at it again.
This code works for the animation, but the audio doesn’t start anymore for some reason.
I am a noob, thanks.

You are pausing and continuing your audio instead of stopping and restarting it. And it is not looping.

1 Like

I tried to do with Stop(); but it also did not work.

What you need is a if-else construct. Because right now, you are always immediately pausing your AudioSource after playing it.

AudioSource audioSource;
Animator animator;

private void Awake()
{
    this.audioSource = GetComponent<AudioSource>();
    this.animator = GetComponent<Animator>();
}

public void GazeEntra()
{
    if(this.audioSource.isPlaying)
    {
        this.animator.Play("quieta");
        this.audioSource.Stop();
    }
    else
    {
        this.animator.Play("cubito");
        this.audioSource.Play();
    }
}
1 Like

this worked :slight_smile: thanks!