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 /
avatar image
0
Question by Adam_15 · Dec 31, 2018 at 10:54 AM · playerprefsaudiosourceslider

How to load audio slider values from PlayerPrefs at the beginning of the game?

Hi everyone!

I have a slight issue with my game , specifically with my audio slider.

The saved audio value is being loaded only when I'm navigating into my menu scenes audio menu (it has the slider childed to it).

The desired function is to load the audio value at the beginning of the game in the start scene but I couldn't figured it out yet how to do it.

Any help is appreciated! :)

Here is the script of my slider:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Audio;
 
 public class MusicSlider : MonoBehaviour {
 
     public AudioMixer audioMixer;
     public Slider musicSlider;
 
 
     public void Start()
     {
         musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
     }
 
 
 
     public void AdjustVolume(float volume)
     {
         PlayerPrefs.SetFloat("Music", volume);
 
         audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
     }
 
 }
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
2
Best Answer

Answer by ray2yar · Dec 31, 2018 at 02:26 PM

Before your music starts, get a volume value from playerprefs. Whenever the player changes the volume during the game set the value in the playerprefs. On game start get the value.

Comment
Add comment · Show 13 · 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 Adam_15 · Dec 31, 2018 at 02:52 PM 0
Share

Thanks for your fast reply.

I tried before to get the value in my start scene in an awake function and It changed the audio sources value but the mixers value was the same and the music was just as loud as it's default value.

Can you please give me a sample code how it looks like?

avatar image ray2yar Adam_15 · Dec 31, 2018 at 04:12 PM 0
Share

Someone just posted code that will work.

$$anonymous$$aybe you're seeing an effect of the slider at value = 0. The result can be a maximum volume in the mixer. Try setting the lower limit of the slider to something really small, but not 0.

avatar image ray2yar Adam_15 · Dec 31, 2018 at 04:13 PM 1
Share

Here is what I wrote up anyways, pretty much the same as the code Tobychappell put in.

     public Slider VolumeSlider;
     public Audio$$anonymous$$ixer Aud;
 
     float volume;
 
     // Use this for initialization
     void Start () {
         if (PlayerPrefs.Has$$anonymous$$ey("$$anonymous$$usic"))
         {
             volume = PlayerPrefs.GetFloat("$$anonymous$$usic");
             VolumeSlider.value = volume;
         }
     }
 
     public void OnVolumeChanged()
     {
         volume = VolumeSlider.value;
         Aud.SetFloat("$$anonymous$$usic", $$anonymous$$athf.Log10(volume) * 20);
         PlayerPrefs.SetFloat("$$anonymous$$usic", volume);
     }
avatar image Adam_15 ray2yar · Jan 01, 2019 at 09:44 AM 0
Share

Thank you!

I will try it out.

Happy New Year! :)

Show more comments
avatar image
2

Answer by Tobychappell · Dec 31, 2018 at 04:02 PM

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.Audio;
  
  public class MusicSlider : MonoBehaviour {
  
      public AudioMixer audioMixer;
      public Slider musicSlider;
  
  
      public void Start()
      {
          musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
          // Set it at the start        <<<<
          audioMixer.SetFloat("Music", Mathf.Log10(musicSlider.value) * 20);
      }
  
  
  
      public void AdjustVolume(float volume)
      {
          PlayerPrefs.SetFloat("Music", volume);
  
          audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
      }
  
  }
Comment
Add comment · Show 6 · 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 Adam_15 · Jan 01, 2019 at 09:45 AM 0
Share

Thank you!

I will try it out.

Happy New Year! :)

avatar image Adam_15 · Jan 02, 2019 at 11:38 AM 0
Share

Unfortunately I still can't figure it out how to get the values in my start scene.

When I try to access the slider from a scrpit in my start scene I'm getting a null reference exception.

Any ideas for that?

avatar image ray2yar Adam_15 · Jan 02, 2019 at 01:33 PM 0
Share

Well that's thrown when it isn't assigned. Did you assign the slider in the inspector or getcomponent?

avatar image Adam_15 ray2yar · Jan 03, 2019 at 09:24 AM 0
Share

I have created a script in my start scene to load the audio value , I tried to find the slider with getcomponent but I still get the null reference exception.

Should I prefab the slider?

Show more comments

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

106 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

Related Questions

PlayerPrefs not saving slider data 3 Answers

Slider value not working? 0 Answers

UI Slider, Save Values and Change Them 0 Answers

Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer

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