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 ThatBenGuy · Aug 13, 2012 at 06:02 AM · slidermusicvolume

Multiple, independent volume sliders

Hello I'm working on a script that allows the player to adjust both the Music volume and Sound Effects volume mid-game through the use of sliders. I've managed to make the Music volume adjustable but I'm not sure how to do so with the Sound Effects volume. I attached an Audio Source, the Audio Clip named 'Song 1', to the player. Attached to various Item-pickups are Audio Source Clips named 'Pickup5' and 'Pickup2' to be adjusted by the Sound Effects volume slider, if that helps.

The code I'm using is:

 GetComponent("Volume - Hard").enabled = false;
 var newSkin : GUISkin;
 var hSliderValue : float = 25.0;
 function OnGUI () {
  
  GUI.Box(Rect(492, 250, 175, 150), "Music");
  
  if(GUI.Button(Rect(550, 365, 65, 30), "Ok")) {
  
  var script1 = GetComponent("Volume - Hard"); 
     script1.enabled = false;
     
 }
  
     hSliderValue = GUI.HorizontalSlider (Rect (520, 280, 120, 30), hSliderValue, 0.0, 50.0);
 }
 
 
 function Update (){
    AudioListener.volume = hSliderValue/50.0;
 }

I have tried repeating the coding for the Music volume slider again, changing the position of the slider, to be used for the Sound Effects slider but to no avail. Also the 'var newSkin: GUISkin;' is not working. I am able to add a GUISkin but the Custom Skin isn't replacing the default GUISkin. I've used this line of code before but this is the first time there is a problem with it.

Thank you very much for any answers or feedback. -Ben

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 fafase · Aug 13, 2012 at 06:12 AM

I don't see the command:

 GUI.skin = customSkin;

to place your new skin.

To alter the sound effect volume, you could fetch the Audio Source on the object and reduce the volume from there.

 var audioSrc:AudioSource[];
 function Start(){
    audioSrc= FindObjectsOfType(AudioSource) as AudioSource[];;
       for (var aud : AudioSource in audioSrc) {
         aud.volume = newValues;
    }
 }

Here it is in the Start but you would call a similar function when you modify the slider: As I reckon you would modify it when game is quiet (not during a battle) or on pause, the find function which should not be used in Update won't create a glitch.

EDIT: It might be that if you instantiate a new object after altering the volume, it will not get the correct level. Your volume should be a static var that you give the value from the slider in the OnGui script and when instantiating a new object in any script use that var to give it the proper value:

 var clone = Instantiate(prefab, position, rotation);
 if(clone.audio)
   clone.audio.volume = GUIScript.staticVolume;

Here I assume the script is called GUIScript and the variable is called staticVolume and is declared in GUIScript.

 GetComponent("Volume - Hard").enabled = false;
 var newSkin : GUISkin;
 var hSliderValue : float = 25.0;
 var previousValue:float= hsliderValue;
 var script1:Volume - Hard;

 function Start(){
   script1 = GetComponent("Volume - Hard"); 
 }
 function OnGUI () {  
   GUI.Box(Rect(492, 250, 175, 150), "Music");
   if(GUI.Button(Rect(550, 365, 65, 30), "Ok")) {//Not sure what is going on here
     script1.enabled = false;
 }
 //Slider gives the value
 hSliderValue = GUI.HorizontalSlider (Rect (520, 280, 120, 30), hSliderValue, 0.0, 50.0);
 // new value is compared to the store value
 // if different the function is called to put all level up/down
 if(previousValue != hsliderValue)Volume();
 // the new value is stored as old value for next frame
 previousValue = hsliderValue;
 }
 // the function that puts all volume up/down
 function Volume(){
    var audioSrc:AudioSource[];
    // Fetch all audio sources and store them in the array declared above    
    audioSrc= FindObjectsOfType(AudioSource) as AudioSource[];
       // Go through each of the audio source and assign the value
       for (var aud : AudioSource in audioSrc) {
         aud.volume = hSliderValue;
    }
 }

Have a go and report if it fails, we will look again together hand in hand.

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 ThatBenGuy · Aug 14, 2012 at 01:30 AM 0
Share

Thank you very much for your answer. I'm a little unsure on how to incorporate the code you provided into my script. Also for some reason 'GUI.skin = customSkin;' doesn't work for me. The console says that 'customSkin' is an 'unknown identifier'

avatar image fafase · Aug 14, 2012 at 05:39 AM 0
Share

yes because I used the general term, you need to replace customSkin with your own variable, newSkin. One way to implement could be to store the value of the slider and compare it with the new one. If different call the function.I update the answer.

avatar image ThatBenGuy · Aug 14, 2012 at 07:47 AM 0
Share

Thank you very much, I really appreciate :). Everything works fine now and the way I need it to work. Just as a reference, in a couple of lines I had to change 'hsliderValue' to 'hSliderValue'. Also what I did now was use the coding you gave me in one script, and the coding I had in my question in another script to have two sliders, one controlling the music and one controlling the sound effects, next to each other.

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

9 People are following this question.

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

Related Questions

Problem implementing volume slider 1 Answer

Need help with my slider 3 Answers

Music play throughout all scenes? 1 Answer

I'm looking for a script allowing Unity to receive information about a music (bpm, frequency, width,...) via OSC. Can anyone refer me? 0 Answers

Volume Slider 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