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 /
  • Help Room /
avatar image
0
Question by shantanusingh994 · Dec 27, 2015 at 10:15 AM · c#unity 5unity5audiosourcevolume

Master Audio source volume control with slider

Hi all, I am a new game developer. I want to setup an audio source in my starting scene which will be the master volume controller , it will control the sound volume of all the audio source in various scenes of my game. I've made the slider its working on the 'start' screen but after moving to other scenes the volume goes back to the set value in the respective scenes (I've multiple audio sources attached to the main camera of each scene). It looks like I've to use a script to handle this. I've made a C# script which takes one main 'AudioSource' and set each audiosource in all the scenes to the main audiosource. Script goes something like this -

 using UnityEngine;
 using System.Collections;
 
 public class ChangeVolumeAll : MonoBehaviour
 {
 
     AudioSource[] asc;
     //private static float vol = GameObject.GetComponents<AudioSource>().volume;
     public AudioSource MasterAudioSource;
     // Use this for initialization
     void Start()
     {
         asc = GetComponents<AudioSource>();
     }
 
     // Update is called once per frame
     void Update()
     {
         SetVolume();
     }
 
     public void SetVolume()
     {
         for (int i = 0; i < asc.Length; i++)
         {
             asc[i].volume = MasterAudioSource.volume;
         }
     }

I think this is working fine but the problem is that when some other scene is loaded from the start scene the volume is getting changed to the original set value. Probably this is due to overwriting of volume details after the scene is getting loaded. How can I achieve the result ?

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

Answer by YourFutureHero · Jan 14, 2016 at 01:08 AM

@shantanusingh994 If you want to set anything up for the player to save you need to use PlayerPrefs or PlayerPrefsManager. I have a "musicManager" which starts at the splash screen to initialize the music. I also have a "levelManager" which calls the level/scene to go to. One other thing, I am using the UI Slider element for my volume control, this is common in mobile development but check out Unity's documentation if you end up using something else to control your volume (i.e. a text field with a number between 1 and 100). I believe this should help as I use it in one of my games but look at the following code setting up an options screen.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class OptionsController : MonoBehaviour {
 
     public Slider volumeSlider;
     public Slider difficultySlider;
     public LevelManager levelManager;
 
     private MusicManager musicManager;
     // Use this for initialization
     void Start () {
         musicManager = GameObject.FindObjectOfType<MusicManager> ();
 
         volumeSlider.value = PlayerPrefsManager.GetMasterVolume ();
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     public void SaveAndExit () {
         PlayerPrefsManager.SetMasterVolume (volumeSlider.value);
         levelManager.LoadLevel ("Start Menu");
     }
 }


If this doesn't work let me know or post your errors and I will attempt to help the best I can.

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 Riphtix · Mar 03, 2016 at 02:18 PM 0
Share

What is in your PlayerPrefs$$anonymous$$anager i would like to use the code for the OptionsController but i dont have the PlayerPrefs$$anonymous$$anager. Also could you tell me where to put these scripts

avatar image YourFutureHero Riphtix · Mar 15, 2016 at 09:59 PM 0
Share

I use this as a basic setup for what is needed with the script above. I have a lot more in my stock one for level unlocks and other things. If you are trying to save more than basic options you will want to write to a file.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class PlayerPrefs$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     const string $$anonymous$$ASTER_VOLU$$anonymous$$E_$$anonymous$$EY = "master_volume";
     const string HIGH_SCORE = "High Score";
 
     public static void Set$$anonymous$$asterVolume (float volume) {
         if (volume >= 0f && volume <= 1f) {
             PlayerPrefs.SetFloat ("master_volume", volume);
         } else {
             Debug.LogError ("$$anonymous$$aster Volume out of range");
         }
     }
 
     public static float Get$$anonymous$$asterVolume () {
         return PlayerPrefs.GetFloat ($$anonymous$$ASTER_VOLU$$anonymous$$E_$$anonymous$$EY);
     }
 
     public static void SetHighScore (int score) {
         PlayerPrefs.SetInt (HIGH_SCORE, score);
     }
 
     public static int GetHighScore () {
         return PlayerPrefs.GetInt (HIGH_SCORE);
     }
 }

PlayerPrefs$$anonymous$$anager just needs to exist in the project. I use it as the script to consolidate my player preferences. So I know where to look for what PlayerPrefs I have. It's only an organizational technique I like to use. If I ever want to add any PlayerPrefs I can go to the script, see what I already have and then tweak it or add to it, versus looking in various scripts that directly talk to the PlayerPrefs. Does that make sense?

Hope this helps!

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

56 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

Related Questions

Player Movement - Not moving at all 3 Answers

Distance is always zero 0 Answers

Stop AudioSource on Trigger!!! 1 Answer

Spawned prefabs are sharing variables values 1 Answer

Properly attach to a GameObject after collision? 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