Trouble with Audio source

Hey!

I have this perplexing problem with the sound effect attached to the win condition.

Here’s a bit of my code:

public class GameController : MonoBehaviour
{
    [SerializeField] GameObject winLabel;
    [SerializeField] float delayBeforeEndingLevel = 10f;

    [SerializeField] AudioClip winSFX;
    [SerializeField] [Range(0, 1)] float winVolume = 0.75f;

    // Start is called before the first frame update
    void Start()
    {
        winLabel.SetActive(false);
    }

    public void EndLevelStuff()
    {
        winLabel.SetActive(true);

        GetComponent<AudioSource>().Play();

        // AudioSource.PlayClipAtPoint(winSFX, this.transform.position, winVolume);

        StartCoroutine("EndLevelAfterDelay");
    }

    IEnumerator EndLevelAfterDelay()
    {
        yield return new WaitForSeconds(delayBeforeEndingLevel);
        FindObjectOfType<LevelLoader>().LoadNextScene();
    }

}

The EndLevelStuff() method is called when there are no existing object of type attacker and the leveltimer is full. It is doing everything else: Activating the win canvas, as well as loading next scene after set time. However it isn’t playing the clip.

The clip works as when I set onAwake on the audioSource, it is playing readily. Also, I went back to the previous projects and implemented audiosource.playclipatpoint. That works, but it repeats the sound.

Any idea why this is happening?

Thanks for your help in advance :slight_smile:

The script that calls the method is:

public class GameTimer : MonoBehaviour
{
    [Tooltip("This is how long it will take for level to end in SECONDS")]
    [SerializeField] float levelTime = 10f;
    



    void Update()
    {
        GetComponent<Slider>().value = Time.timeSinceLevelLoad / levelTime;

        bool timerFinished = (Time.timeSinceLevelLoad >= levelTime);

        

        AttackerSpawner[] attackerspawners = FindObjectsOfType<AttackerSpawner>();


        if(timerFinished)
        {
            foreach (AttackerSpawner attackerspawner in attackerspawners)
            {
                attackerspawner.TurnOffSpawning();
            }


            DoThingsToEndScene();
        }
    }

    private void DoThingsToEndScene()
    {
        Attacker[] attackers = FindObjectsOfType<Attacker>();
        int currentNumberOfAttackers = attackers.Length;
        if (currentNumberOfAttackers <= 0)
        {
            FindObjectOfType<GameController>().EndLevelStuff();
        }
    }
}

Maybe because it is calling stuff on update, it is called per frame and the audio does not work? I’m not sure why the coroutene works but the sound does not.

try

GetComponent<AudioSource>().PlayOneShot(winSFX);

instead of

       GetComponent<AudioSource>().Play();

That’s exactly what’s happening, you’ll need to find a way to prevent the audio source from Playing during every frame, the simplest way might be to add a bool to your if statement so it only works once.

Here's an example in case you need it
if(timerFinished && !alreadyTriggered)
{
    alreadyTriggered = true;

The reason why you are not hearing anything is that the AudioSource gets interrupted every frame, if you use other methods to play the audio you’ll end with some really nasty sound because it will play a new sound every frame, so you’ll have a bunch of audio clips playing on top of the other.

As to why the coroutine works. You are creating a bunch of coroutines with that code, but since there’s a timer and a change of scenes it looks like you are only creating one, when changing scenes those coroutines get deleted so they never trigger.

Hope this helps!

1 Like

I tried that. It didn’t help. I guess the problem is me calling it on update. Thank you for your help though :slight_smile:

Thank you so much. I changed the way the end is handled so it is called on the last enemy dying instead of on update. Now it works fine.

Before I changed that, another way for it to work was to add the audiosource to the ending canvas. Since it got activated only at the end and the clip played onawake, it approximated the same thing. OfCourse, misusing the update is bad, so I changed the script too and now use a boolean instead.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms