Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to unmute at a precise moment in sound track

Discussion in 'Scripting' started by TozerAuthor, Nov 16, 2016.

  1. TozerAuthor

    TozerAuthor

    Joined:
    Nov 4, 2016
    Posts:
    11
    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.

    Code (CSharp):
    1. //FUNCTION TO MUTE AND UNMUTE MUSIC
    2.     public void MuteMenuMusic()
    3.     {
    4.         if (IsMusicMuted == false)
    5.         {
    6.            
    7.             CurrentSoundImage.color = Color.red; //CHANGE BUTTON COLOR TO RED
    8.             AudioSourcePlayer.mute = !AudioSourcePlayer.mute; //MUTE MUSIC
    9.             IsMusicMuted = true;
    10.  
    11.         }
    12.         else if (IsMusicMuted == true)
    13.         {
    14.             AudioSourcePlayer.PlayScheduled(0.00f);
    15.             AudioSourcePlayer.mute = !AudioSourcePlayer.mute; //UNMUTE MUSIC
    16.  
    17.             CurrentSoundImage.color = Color.white; //CHANGE BUTTON COLOR TO WHITE
    18.            
    19.             IsMusicMuted = false;
    20.         }
    Thank you to all, and happy coding. :)
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you want it to continue from the point that it was muted wouldn't that functionally be a pause and not a mute?
     
  3. TozerAuthor

    TozerAuthor

    Joined:
    Nov 4, 2016
    Posts:
    11
    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. :)
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  5. TozerAuthor

    TozerAuthor

    Joined:
    Nov 4, 2016
    Posts:
    11
    Thank you, KelsoMRK. You have been a wonderful help.

    Have a great day, whatever you do. :)