Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wasicool7 · Aug 21, 2016 at 05:22 PM · audiosourceaudioclipmute

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wasicool7 · Aug 21, 2016 at 07:28 PM 0
Share

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);
avatar image TBruce wasicool7 · Aug 21, 2016 at 08:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges