Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 dariusneag04 · Nov 23, 2021 at 08:03 PM · audiosoundmusicvolumeaudio source

Audio slider for multiple scenes.

Hello, I'm working on my first game project. I've made the main menu and I've added a slider for the volume. My problem is that the volume slider onlys work for main menu scene and I want to work with every scene but I don't know how to do that. Here's the code so far

 public class VolumeValueChanger : MonoBehaviour
 {
     
     private AudioSource audioSrc;
     
 
     [SerializeField] Slider volumeSlider;
     
     private float musicVolume = 1f;
 
     
     void Start()
     {
 
         
         audioSrc = GetComponent<AudioSource>();
         
     }
     
     void Awake()
     {
         if (PlayerPrefs.HasKey("Volume"))
         {
             SetVolume(PlayerPrefs.GetFloat("Volume"));
             volumeSlider.value = PlayerPrefs.GetFloat("Volume");
         }
     } 
 
     
     void Update()
     {
 
         audioSrc.volume = musicVolume;
         
     }
 
     
     public void SetVolume(float vol)
     {
         musicVolume = vol;
         PlayerPrefs.SetFloat("Volume", vol);
         
     }
 }








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
0
Best Answer

Answer by Hellium · Nov 23, 2021 at 08:43 PM

First of all, get rid of your script.

PlayerPrefsFloatLoader.cs

To be attached to any gameObject, preferably to your AudioSource

Add AudioSource > volume (dynamic) to the onValueLoaded event

Add Slider > value (dynamic) to the onValueLoaded event only in the main scene

Set the key to Volume

 using UnityEngine;
 using UnityEngine.Events;
 
 [System.Serializable]
 public class FloatEvent : UnityEvent<float> { }
 
 public class PlayerPrefsFloatLoader : MonoBehaviour
 {
     [SerializeField] private string key;
     [SerializeField] private float defaultValue = 0;
     [SerializeField] private FloatEvent onValueLoaded;
 
     private void Awake()
     {
         onValueLoaded.Invoke(PlayerPrefs.GetFloat(key, defaultValue));
     }
 }

PlayerPrefsFloatSaver.cs

To be attached to any gameObject, preferably to your Slider

Add PlayerPrefsSaver > SetFloat (dynamic) to the onValueChanged event of the Slider in the main scene

Add AudioSource > volume (dynamic) to the onValueChanged event of the Slider in the main scene

Set the key to Volume

 using UnityEngine;
 
 public class PlayerPrefsFloatSaver : MonoBehaviour
 {
     [SerializeField] private string key;
     
     public void SetFloat(float value)
     {
         PlayerPrefs.SetFloat(key, value);
     }
 }



alt text

Right click on image > Open in new tab if it's too small for you to read


audiosourcesave.png (114.3 kB)
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 raxashafique · Nov 23, 2021 at 10:08 PM

Since you are writing it in a PlayerPref, and I am assuming that you have another VolumeValueChanger in your other scene as well. Based on the above assumption you can Implement the SceneLoaded Event from Unity SceneManager and re-load the value from PlayerPrefs.

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

P.S. Two quick Solutions:

  1. Make your object Don't Destroy On Load >> In order for it to Persist between scenes and retain its values.

  2. Make a sound manager class, and make it Singleton that also doesn't destroy on load

More info in Don't Destroy On Load here: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

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

170 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 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 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 avatar image avatar image

Related Questions

Why is my backtrack's volume getting lower? 0 Answers

Increase the volume of sound source past limit? 1 Answer

3D sound doesn't fade out when moving away from audio source 0 Answers

Mute volume / sound problem 1 Answer

Low sound volume on phone speaker but normal on headphone 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