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 /
avatar image
0
Question by kardamzaimov · May 13, 2018 at 03:08 PM · audiosoundaudiosourcemute

mute/un-mute

Hi!

I have the following script, which attached to an object with audio source, un-mutes the audio upon collision with FPPlayer.

How should I modify the script to mute the audio after a second collision and mute/un-mute it on every next (like a on/off button upon colliding)?

  using UnityEngine;
  
  // make sure the game object you attach it to has an AudioSource on it
  [RequireComponent(typeof(AudioSource))]
  // create a script that you can attach to a game object
  public class SetVolumeOnCollision : MonoBehaviour {
  
      // the volume to set when collided; can be set in the Editor; 
      // not necessarily needed, but it will make the component more versatile
      [SerializeField] private float volumeOnCollision = 1;
  
      // the tag to which it should react; also not essential, but nice to have
      // make sure you set the tag of your colliding object if you do use it!
      [SerializeField] private string tagToReactTo = "Player";
  
      // the audio source you want to control
      private AudioSource audioSource;
  
      // this is called in the first moment, even before sounds start to play
      private void Awake () {
          // get the audio source to control that is on this game object
          audioSource = GetComponent<AudioSource>();
          // set volume to 0, muting it; you can also do this in the Editor
          audioSource.volume = 0;
          // make sure it wil start to play; this as well can be set in the Editor
          audioSource.playOnAwake = true;
      }
  
      // this function is called on the frame when something collides with this game object
      private void OnCollisionEnter (Collision collision) {
          // check the tag; remove this if you don't want it
          if (collision.collider.CompareTag(tagToReactTo))
              // set the desired voluem; just replace it with 1 if you don't want to use desired volume
              audioSource.volume = volumeOnCollision;
      }
  
  }
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 Glurth · May 13, 2018 at 06:22 PM

Add a boolean member variable to the class, which records the current muteState;

 bool muteState=false;

Also, add a float to record the originalVolume of the audio source, before you mute it.

 float originalVolume;


Now, in OnCollisionEnter, you can Set the volume Based upon this:

 if( ! muteState) //if not muted, mute it
 {
    originalVolume= audioSource.volume ;
    audioSource.volume =volumeOnCollision; //muted  
 }
 else //if already muted, UN-mute it
 {
    audioSource.volume = originalVolume; 
 }

Then toggle the muteState like so:

 muteState= ! muteState; 
Comment
Add comment · Show 8 · 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 kardamzaimov · May 13, 2018 at 07:24 PM 0
Share

thank you! can you combine it with the code I already have, I seem to have troubles doing it...

avatar image Glurth kardamzaimov · May 13, 2018 at 07:52 PM 0
Share

I'm happy to help, but no, I'm not going to do it FOR you.

avatar image kardamzaimov Glurth · May 13, 2018 at 08:27 PM 0
Share

I don't have coding experience, I can't do it myself in the near future. I'm would not ask for help If I could do it myself for less than 24 hours.

Show more comments

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

108 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 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

Is there a way to create a random Audiosource loop? 2 Answers

Realistic Sound Effect 0 Answers

Audio Loops? 1 Answer

Audio in WEBGL on IOS devices -1 Answers

PlayOneShot returns false for isPlaying 5 Answers


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