How to unmute at a precise moment in sound track

Hello all,

Hope everyone is doing well? I have this small problem. I have created a mute/unmute button for my game, whereby it changes color (red) when turned on and to (white) when off. The music track plays fine, albeit for one hiccup. The track restarts whenever I unmute the sound.

Here is my code for the function. By the way I am using a button to switch between mute and unmute. I would like to know how I can get the music to play at the precise moment I muted it at.

//FUNCTION TO MUTE AND UNMUTE MUSIC
    public void MuteMenuMusic()
    {
        if (IsMusicMuted == false)
        {
           
            CurrentSoundImage.color = Color.red; //CHANGE BUTTON COLOR TO RED
            AudioSourcePlayer.mute = !AudioSourcePlayer.mute; //MUTE MUSIC
            IsMusicMuted = true;

        }
        else if (IsMusicMuted == true)
        {
            AudioSourcePlayer.PlayScheduled(0.00f);
            AudioSourcePlayer.mute = !AudioSourcePlayer.mute; //UNMUTE MUSIC

            CurrentSoundImage.color = Color.white; //CHANGE BUTTON COLOR TO WHITE
           
            IsMusicMuted = false;
        }

Thank you to all, and happy coding. :slight_smile:

If you want it to continue from the point that it was muted wouldn’t that functionally be a pause and not a mute?

Hello Kelso,

Thank you for the reply. I have attempted to use the Pause() function with an AudioSource, although, the music still returns to the beginning.

Thank you, however, and enjoy the rest of your day. :slight_smile:

Weird. In either case - you can just grab the current time before pausing (Unity - Scripting API: AudioSource.time) and then use PlayScheduled with that time when you unpause.

Also - PlayScheduled(0) is basically the same as Play() :slight_smile:

Thank you, KelsoMRK. You have been a wonderful help.

Have a great day, whatever you do. :slight_smile: