🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

can't stop an audio clip? (AudioSource.Stop() not working) unity C#

Started by
4 comments, last by SassyPantsy 3 years, 7 months ago

hello!

i'm trying to make a proximity based SFX, where an NPC calls for the player when he enters his collider.

that works fine. the problem is, that when the player starts the interaction, i want the call audio clip to stop so that the conversation wouldn't overlap. i've created a script that handles the audio, and i'm trying to test the simple .Stop() function with an input, but the clip won't stop. adding the script.

character naming is because i'm re-creating a witcher 2 quest for practice

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ElthonSFX : MonoBehaviour
{
    public AudioSource elthonCallingGeralt = null;//component is inserted via inspector

    void Start()
    {
        
    }

    void Update()
    {
		if (Input.GetKeyDown(KeyCode.O))//for testing .Stop()
		{
            StopCalling();
		}
    }

    public void CallGeralt()//called from another script via OntriggerEnter()
    {
        AudioSource.PlayClipAtPoint(elthonCallingGeralt.clip, transform.position); //works
    }

    public void StopCalling()
    {
        elthonCallingGeralt.Stop();//doesn't work
        print("stoppedCalling");//printing works
    }
}

thanks

None

Advertisement

You can't call Stop when you use the Play-functions that directly take the “clip”. This applies to a lot of functions on the source (isPlaying, …), for some reason (the reason IMHO being that unity is stupid). Anyhow, if you want to stop a clip, you need to use the AudioSource.Play()-function (the one without parameters), which requires you to assign the clip to “AudioSource.clip” first (eigther in the inspector or in code before Play). You then obviously need a different AudioSource per clip that could be played in parallel.

Juliean said:

You can't call Stop when you use the Play-functions that directly take the “clip”. This applies to a lot of functions on the source (isPlaying, …), for some reason (the reason IMHO being that unity is stupid). Anyhow, if you want to stop a clip, you need to use the AudioSource.Play()-function (the one without parameters), which requires you to assign the clip to “AudioSource.clip” first (eigther in the inspector or in code before Play). You then obviously need a different AudioSource per clip that could be played in parallel.

what exactly do you mean "assign the clip to AudioSource.Clip first?

meaning if i want to stop the clip before it finishes playing out, i nee to change the variable from AudioSource to AudioClip?

also, does that mean i can't use the .PlayClipAtPoint? that really sucks if so… seems odd as well, because a lot of ambient noise in games is location based and needs to be able to be stopped before it finishes (like opening and closing a sink in a stealth game, for a random example)

None

SassyPantsy said:
what exactly do you mean

SassyPantsy said:
also, does that mean i can't use the .PlayClipAtPoint? that really sucks if so… seems odd as well, because a lot of ambient noise in games is location based and needs to be able to be stopped before it finishes (like opening and closing a sink in a stealth game, for a random example)

Yes, thats what i means. AudioSource.PlayClipAtPoint creates a new AudioSource, and you try to call “stop” on another source. On second look, it seems you already have an AudioSource:

 public AudioSource elthonCallingGeralt = null;

So you just need to call “Play" on this, and move the GameObject to the position you want to. For example, you can do it with this code:

elthonCallingGeralt.Play();
elthonCallingGeralt.transform.position = transform.position;

You could write your own variant of PlayClipAtPoint that returns a AudioSource, on which you can call stop, but I just don't have the time to block that out right at them moment, sorry.

@Juliean ok i'll try that. atm i'm having trouble with stopping audio in general so i think i'll go look for a more extensive unity audio tutorial so i could get some knowledge gaps closed.

None

This topic is closed to new replies.

Advertisement