Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by doublegumbo · Nov 07, 2013 at 03:10 PM · soundbasicaccessing from any scriptmute

How do I mute one sound source using another?

I have a cube which emanates sound. I want to make it so that when my First-person camera controller passes through another cube, the sound of the first cube mutes.

The challenge I'm facing here is that I think I need to place a script on the second cube that points to and controls the mute controls on the first cube.

This seems pretty basic, but I don't know the way forward. I've tried using the Accessing Other Game Objects tutorial and was able to locate the first cube from the second using a tag. However, I'm not entirely sure where to go from here or whether that was even the right approach to begin with.

Any help would be appreciated! For what it's worth, I'm using C Sharp but am open to using either JS or CS for this.

thanks

-g

Comment
Add comment · Show 3
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 DannyB · Nov 07, 2013 at 04:07 PM 1
Share

I think you are on the right track. If it is just two objects you want to communicate with each other, I would simply create a public void $$anonymous$$uteSound() or similar on one, and have the other call it as needed.

For this approach, I would avoid using tags, ins$$anonymous$$d have a public YourScriptName otherCube variable in the caller cube, and set it via the inspector, so you would be able to simply do otherCube.$$anonymous$$uteSound()

But, all this is a little too rigid, if you want to have more objects. In this case, I would use events. If you are new to this, and don't want to spend too much time on learning it, you may want to start with Send$$anonymous$$essage and only later try your luck with events.

avatar image Crazydadz · Nov 07, 2013 at 04:28 PM 1
Share

CubeA = cube with sound CubeB = cube muting CubeA sound

Add collider on cubeB and set it to isTrigger. Put a script on it like so:

 public AudioSource cubeASound;
 
 private void OnTriggerEnter(...){
  cubeASSound.mute = true;
 }

You need a ridgidbody to be able to catch triggers. So put a rigidbody on CubeB or on the object passing through CubeB.

avatar image doublegumbo · Nov 08, 2013 at 02:46 PM 0
Share

This worked like a charm, thanks! Creating the object cubeASound was the missing step for me.

1 Reply

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

Answer by Tomer-Barkan · Nov 07, 2013 at 04:21 PM

Put a script on your character that keeps a reference to the current playing source. When colliding with the new source, stop the old one, start the new one, and keep a reference to the new one instead.

 private AudioSource currentlyPlaying = null;

 void OnTriggerEnter(Collider other) {
     AudioSource newSource = other.GetComponent<AudioSource>();

     // if collided with an object that has audio source
     if (newSource != null) {
         // disable previous source if exists
         if (currentlyPlaying != null) {
             currentlyPlaying.Stop();
         }

         // activate new source and keep reference
         currentlyPlaying = newSource;
         currentlyPlaying.Play();
     }
 }
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 doublegumbo · Nov 07, 2013 at 05:44 PM 0
Share

This is really helpful, thanks everyone! I'll let you know how it goes. -g

avatar image Crazydadz · Nov 07, 2013 at 06:36 PM 0
Share

Don't forget to accept the answer please.

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

18 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

Related Questions

mute / unmute helps 2 Answers

Mute unity app if another external app is playing music in the background on iOS 0 Answers

Mute Button doesn't work?! 1 Answer

Function that does not affect him Time.timeScale? 1 Answer

Lost sound on iphone after alarm clock goes off 0 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