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 Sskethan · Jun 21, 2016 at 04:56 PM · soundsliderscenesvolume

How to save volume when switching scene?

Hello, I have been reading through other questions regarding this and was able to get the sound to change between scenes but when going back to the main menu where the volume slider is located, the volume slider is back to full and the slider no longer changes the volume. How can I save the position of the slider and change the sound from this point? Any help appreciated, Thank you.

     public static AudioManager Instance;
 
     void Awake()
     {
         if(Instance)
             DestroyImmediate(gameObject);
         else
         {
             DontDestroyOnLoad(gameObject);
             Instance = this;
         }
     }
 
     public void MusicVolume(float musicControl)
     {
         if (FindObjectOfType<AudioManager>())
         {
             GetComponent<AudioSource>().volume = musicControl;
         }
     }
 }
Comment
Add comment · Show 4
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 awest · Jun 21, 2016 at 05:18 PM 0
Share

It might be your $$anonymous$$enu code. It should read the volume from your Audio$$anonymous$$anager to initialize.

In your above code I don't understand why you have that if statement in the $$anonymous$$usicVolume method. You would not be able to call the method unless the Audio$$anonymous$$anager existed.

avatar image Dibbie · Jun 21, 2016 at 05:44 PM 0
Share

Your also never calling your $$anonymous$$usicVolume function - why not just temperately store the volumes value in a PlayerPrefs SetFloat and then use GetFloat after on the next scene load? $$anonymous$$ight be a little bit easier,and that way you dont have to handle not destroying any objects from scene to scene, its all stored in the code.

avatar image Sskethan · Jun 21, 2016 at 05:51 PM 0
Share

I added the if statement there because i was getting duplicate audiomanagers when going back to my main menu.

avatar image awest Sskethan · Jun 21, 2016 at 06:40 PM 0
Share

I mean this if statement.

if (FindObjectOfType-Audio$$anonymous$$anager-())

It checks if there is any Audio$$anonymous$$anager before setting the volume. So if there is 1 or 8 it returns true, but it can never return false because the method exists within an Audio$$anonymous$$anager.

2 Replies

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

Answer by TBruce · Jun 21, 2016 at 06:01 PM

If you don't use DontDestroyOnLoad() the the audio volume will always start at the same level when loading a scene.

What you want to do instead is save the volume level set by the slider to PlayerPrefs and get this value on loading a scene. So you can do something like the script below

This would be on the game scenes themselves

 // you may want to add the RequireComponent() attribute shown below to this script
 // [RequireComponent(typeof(AudioSource), typeof(AudioSource))]

 AudioSource audioSource;
 
 void Start ()
 {
     audioSource = GetComponent<AudioSource>();
 
     // get the float value of SliderVolumeLevel if it has been saved with PlayerPrefs.SetFloat()
     // else use defult value of audioSource.volume
     audioSource.volume = PlayerPrefs.GetFloat("SliderVolumeLevel", audioSource.volume);
 }

this would be added to the script with the Slider on the main menu.

 float volume = 0.5f; // AudioSource.volume will have a value 0.0f to 1.0f
 void SaveSliderValue()
 {
     PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
 }
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 Sskethan · Jun 21, 2016 at 07:05 PM 0
Share

Thank you, Not sure i understand. If i don't use DontDestroyOnLoad then how do i get the audio$$anonymous$$anger to Instansiate in each scene? Or would i add an Audio$$anonymous$$anger to every scene? When i try that the sound just resets to full volume on each scene.

avatar image TBruce Sskethan · Jun 21, 2016 at 07:37 PM 0
Share

Personally I would not make Audio$$anonymous$$anger static in the first place. And yes, I would add Audio$$anonymous$$anger to every scene.

Having said that if you save the "SliderVolumeLevel" in in the main menu using PlayerPrefs. You can then load it as shown in the Start() function above as seen below.

 // this statement either sets audioSource.volume to the PlayerPrefs SliderVolumeLevel if it exists or
 // it leaves it at the default value
 audioSource.volume = PlayerPrefs.GetFloat("SliderVolumeLevel", audioSource.volume);
avatar image
0

Answer by ConsoleHack000 · Nov 04, 2016 at 05:42 PM

There are many ways to do this. One is PlayerPrefs, another one is a static variable, or you can use dontdestroyonload.

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

60 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

Related Questions

Play test sound when slider's value is changed and mouse button got released. 0 Answers

Adjusting volume of an audio source from a previous scene 1 Answer

Adjusting Audio Volume via UI Slider 0 Answers

How to change the rotation of a camera in another scene 0 Answers

How Do I Make A Change A Volume With A 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