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 ![OnClick][1]
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 a
field' but a type' was expected and error CS0118:
UI_ManagerScripts.Gamestartsound' is a field' 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);
}
}
Answer by TBruce · Aug 21, 2016 at 06:31 PM
Try this approach
using UnityEngine;
using System;
public class UI_ManagerScripts : MonoBehaviour
{
public AudioClip Gamestartsound;
public GameObject Music;
public PlayerMovement playerMovementRef;
public bool playSoundOnAwake = false; // change this as necessary
public bool loopSound = true; // change this as necessary
private AudioSource audioSource;
void Start()
{
// get a reference to an AudioSource component
// do this here once
if (GetComponent<AudioSource>() == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
else
{
audioSource = gameObject.GetComponent<AudioSource>();
}
audioSource.playOnAwake = playSoundOnAwake;
audioSource.loop = loopSound;
if (playSoundOnAwake)
{
PlayGamestartSound();
}
}
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 ()
{
audioSource.mute = !audioSource.mute;
}
public void ExitGame()
{
Application.Quit ();
}
public void PauseGame()
{
Time.timeScale = 0;
playerMovementRef.enabled = false;
}
public void UnPauseGame()
{
Time.timeScale = 1;
playerMovementRef.enabled = true;
PlayGamestartSound();
}
public void PlayGamestartSound()
{
audioSource.PlayClipAtPoint (Gamestartsound, transform.position);
// consider using this instead
// audioSource.PlayOneShot(GamestartSound);
}
}
Muting is done through the AudioSource. The code above adds one to the current GameObect in the Start() function if one does not already exist and gets a reference to it. It then uses it to mute and play the Gamestartsound AudioClip.
It is not apparent from your code if Gamestartsound is background music or not so two public variable were added to initialize the AudioSource properly
public bool playSoundOnAwake = false; // change this as necessary
public bool loopSound = true; // change this as necessary - if not Background music set to false
Thank you for the replay , I seem to be getting an error: error CS0176: Static member `UnityEngine.AudioSource.PlayClipAtPoint(UnityEngine.AudioClip, UnityEngine.Vector3)' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d.
With:
audioSource.PlayClipAtPoint (Gamestartsound, transform.position);
PlayClipAtPoint() is a static function hence it neds to be used the way you were originally using it
AudioSource.PlayClipAtPoint (Gamestartsound, transform.position);
But because you need to be able to mute the sound as well as have more control over the audio itself you should be using
audioSource.PlayOneShot(GamestartSound);
as suggested above.
Because the audioSource reference is attached to the current GameObject the result is exactly the same as
AudioSource.PlayClipAtPoint (Gamestartsound, transform.position);
you just have more control over your audio.
Your answer
Follow this Question
Related Questions
On iOS Build, Audio Gets Muted When App Loses Focus 0 Answers
How to mute multi-able audioclips within a single audiosource? 0 Answers
Please help! Audio not working in another class.. 0 Answers
Getting nullReferenceException when respawning a gameObject and trying to access the audio source. 0 Answers
How to Play a Sound Twice in a Row 1 Answer