0
\$\begingroup\$

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

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)
{
    Application.LoadLevel ("Game Level");
    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);
}

}

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Jessca Stone Aug 21 '16 at 17:46
  • \$\begingroup\$ It has a boolean property playOnAwake so you can control it. Actually I misspelled the component name, it seems to be just AudioSource. \$\endgroup\$ – SurvivalMachine Aug 21 '16 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.