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
1
Question by Dima Antonov · Feb 28, 2010 at 09:07 AM · audiovolumesetting

Setting audio volume with horizontal slider (to many AudioSources with different volume)

I have a number of audio sources and they all have different volume. Initial idea was to find all sources with FindObjectsOfType(AudioSource) and if slider is set to 0.2, I calculate 20% volume for each with this formula volume * sliderValue

But later it appeared that if user sets slider to zero and then tries to set higher volume, volume stays zero. So I understood I need a variable to store initial volume and the formula will be initialVolume * sliderValue.

As I didn't find easy way to add a public variable to AudioSource component, I decided to add component AudioVolumeInitial.js (that contains only one line static var initialVolume = 0.0;) for each AudioSource with my script (of course I could add this component manually with inspector panel, but that seemed like a hassle to me).

So here is piece of code from my AudioVolume.js. This piece is enough to demonstrate my problem.

function Start () {
    var sources = FindObjectsOfType(AudioSource);
    for (var source : AudioSource in sources) {
        source.AddComponent("AudioVolumeInitial");
        AudioVolumeInitial.initialVolume = source.audio.volume;
        source.audio.volume = AudioVolumeInitial.initialVolume * GUIMainMenu.musicVolume;
    }
}

This line is problematic one so far source.AddComponent("AudioVolumeInitial"); how do I add component to the parent if all I have is AudioSource components in my array? I tried source.transform.parent.AddComponent("AudioVolumeInitial"); but it didn't work.

Anyway the best solution would be to find an easy way to get to the parent (that's what I want you to help me with). 2nd option is to attach component AudioVolumeInitial manually (boring, but I can do this if there no other way). And 3rd if I could just add one more public variable to AudioSource component (no idea how, help please).

That's all there is. Thanks.

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

2 Replies

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

Answer by Dima Antonov · Mar 01, 2010 at 11:29 AM

I managed to fix this with maxVolume of the AudioSource, I used it to store initial volume. Couple of extra things appeared to be need to correct volumes, which were set in other scripts. But don't mind that. Main code it this (I also added check for tag to know if it's music or sound effects):

function Start () { var sources = FindObjectsOfType(AudioSource); for (var source : AudioSource in sources) { if( source !== GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) { source.maxVolume = source.volume;//set this only on start, we keep initial volume inside maxVolume source.volume = source.maxVolume * GUIMainMenu.effectsVolume; } else if ( source == GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) { source.maxVolume = 1;//set this only on start, we keep initial volume inside maxVolume source.volume = source.maxVolume * GUIMainMenu.musicVolume; } } }

function GlobalVolume() { var sources = FindObjectsOfType(AudioSource); for (var source : AudioSource in sources) { if( source !== GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) { source.volume = source.maxVolume GUIMainMenu.effectsVolume; } else if ( source == GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) { source.volume = source.maxVolume GUIMainMenu.musicVolume; } } }

Comment
Add comment · 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
0

Answer by StephanK · Feb 28, 2010 at 12:43 PM

Your problem is probably in line 5. If you set AudioVolumeInitial.initialVolume this won't affect the instance of the component you just created. Instead try this:

AudioVolumeInitial av = source.AddComponent("AudioVolumeInitial") as AudioVolumeInitial;
av.initialVolume = source.audio.volume;

This is C# syntax, I don't use js so I'm not sure about the syntax there but the point is to store the return value of AddComponent as a AudioVolumeInitial component and use that to set the volume.

Comment
Add comment · Show 1 · 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 Dima Antonov · Mar 01, 2010 at 11:22 AM 0
Share

No, in this everything seemed okay in JS.

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

No one has followed this question yet.

Related Questions

Inconsistent ReverbZone / AudioSource.volume behavior 2 Answers

Breathing volume increases as player gets more tired 0 Answers

Fading out before load script not functioning correctly? 2 Answers

Volume rolloff: Distance to listener not changing. 3 Answers

Math formula to set volume levels nicely 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