# Unity2D: Muting audio Clip problems

I'm trying to create a mute button (that goes both way, e.g mute and un-mute) using button onclick

So far I'm not having the greatest luck. I made this:

  mute = !mute;
if (mute){
gameObject.GetComponent<Gamestartsound>().volume = 0;
}else
{
gameObject.GetComponent<Gamestartsound>().volume = 0;
}


To achieve what I'm trying to do but getting errors:

error CS0118: UI_ManagerScripts.Gamestartsound' is afield' but a type' was expected

and

error CS0118: UI_ManagerScripts.Gamestartsound' is afield' but a type' was expected

anyway this is my full UIManager script, I did the audio like that because I was having some issues earlier but it's better now so yeah:

public AudioClip Gamestartsound;
public GameObject Music;
public PlayerMovement playerMovementRef;
private bool mute;

public void DisableBoolAnimator(Animator anim)
{
anim.SetBool ("IsDisplayed", false);
}

public void EnableBoolAnimator(Animator anim)
{
anim.SetBool ("IsDisplayed", true);
}

public void NavigateTo(int scene)
{
Movement.Restart ();
}
public void Mute ()
{
mute = !mute;
if (mute){
gameObject.GetComponent<Gamestartsound>().volume = 0;
}else
{
gameObject.GetComponent<Gamestartsound>().volume = 0;
}
}

public void ExitGame()
{
Application.Quit ();
}
public void PauseGame()
{
Time.timeScale = 0;
playerMovementRef.enabled = false;
}

public void UnPauseGame()
{
Time.timeScale = 1;
playerMovementRef.enabled = true;
AudioSource.PlayClipAtPoint (Gamestartsound, transform.position);
}


}

Consider this line:

gameObject.GetComponent<Gamestartsound>().volume = 0;


Gamestartsound is not a component, that's why it doesn't compile. I'd fix this by adding an AudioSourceComponent into the game object in inspector and setting its clip to whatever clip Gamestartsound refers. Then you can change the line into:

gameObject.GetComponent< AudioSource >().volume = 0;


You are also setting the volume to 0 in both branches:

if (mute){
gameObject.GetComponent<Gamestartsound>().volume = 0;
}else
{
gameObject.GetComponent<Gamestartsound>().volume = 0;
}


The latter should probably be set to 1.

• Thank you for your reply the Gamestartsound is used because I wanted the ausio clip to be called when I click on a button, So its only natural that if the user would want to mute that sound. So would adding a AudioSourceComponent work? Would it play onAwake or play when I click a button??? Thank you :) – Jessca Stone Aug 21 '16 at 17:46
• It has a boolean property playOnAwake so you can control it. Actually I misspelled the component name, it seems to be just AudioSource. – SurvivalMachine Aug 21 '16 at 17:54