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
0
Question by Sam 9 · May 11, 2011 at 08:22 AM · slidervolume

Volume Slider - Need to know how to link it to Audio.Source

Hello everyone,

I'm making a racing game and on the main menu I want to have a volume slider so the player can adjust the volume from simply sliding it from right to left.

I currently have the Slider created and in position, I just need to know how to link it to the Audio.source?

Here's what I have so far:

var hSliderValue : float = 0.0;

function OnGUI () { hSliderValue = GUI.HorizontalSlider (Rect (370, 220, 546, 30), hSliderValue, 0.0, 10.0); }

Any help would be appreciated!

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

5 Replies

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

Answer by Dreamer · May 11, 2011 at 08:54 AM

function Update(){
   AudioListener.volume = hSliderValue/10.0;
}

(10.0 is your slider bar max value.)

Comment
Add comment · Show 3 · 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 CHPedersen · May 11, 2011 at 08:58 AM 2
Share

Or he could simply set the range of the HorizontalSlider to 0-1.0 ins$$anonymous$$d of 0-10.0, and then dividing by 10 again when using the value...

avatar image Sam 9 · May 11, 2011 at 09:09 AM 0
Share

Thank you. All sorted.

avatar image Jessy · May 21, 2011 at 09:51 PM 0
Share

This should make it easier to use: http://www.unifycommunity.com/wiki/index.php?title=Loudness

avatar image
0

Answer by CHPedersen · May 11, 2011 at 08:28 AM

Can't you simply adjust the volume-property of the audio source?

From the docs:

public class example : MonoBehaviour {
    void Awake() {
        audio.volume = 0.2F;
    }
}

http://unity3d.com/support/documentation/ScriptReference/AudioSource-volume.html

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 Eric5h5 · May 11, 2011 at 10:01 AM 0
Share

That would set the volume for a single audio source. If you set the volume for the audio listener, then it's an overall volume control, which seems to be what the question is asking for. (Then you can use AudioSource.ignoreListenerVolume for the music, which you'd likely want to be controlled separately.)

avatar image
0

Answer by androids · Sep 23, 2013 at 01:27 PM

 var hSliderValue : float = 0.0;
  
 function OnGUI () {
     hSliderValue = GUI.HorizontalSlider (Rect (370, 220, 546, 30), hSliderValue, 0.0, 10.0);
     audio.volume = hSliderValue;
 }
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 Penra · Sep 20, 2015 at 10:35 PM

Couldn't you just link the two values using the slider's "On Value Changed" event?

Add a new event to the slider's "On Value Changed" section, drag your audio source to it, then select AudioSource.volume (the one in dynamic float, not static parameters). That should link the two values without requiring an extra script.

I found a YouTube video that used this method: https://www.youtube.com/watch?v=gK-8n3IZlJo

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 BrunoOre · Oct 04, 2016 at 02:24 PM

Volume / Slinder.Value * Silinder.MaxValue

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple, independent volume sliders 1 Answer

Music play throughout all scenes? 1 Answer

Controlling Master Volume with one slider? 1 Answer

how to change and save audio volume from a different scene with a slider 1 Answer

The volume does not reduce when master volume is lowered using volume slider 3 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