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
1
Question by Smithy5995 · Apr 07, 2017 at 09:29 PM · audioaudiosourceslidervolume

Volume Slider not saving when changing scene

So I'm very new to Unity C# programming and I'm having a little bit of a problem with the background audio not saving when loading a new scene and then going back to the original scene.
I've heard about Singleton and Don'tDestoryOnLoad() but I still don't fully understand how it works.
The code that I've tried I got from multiple Google searches but it still doesn't work:

 [RequireComponent(typeof(AudioSource), typeof(AudioSource))]
  public class VolumeSlider : MonoBehaviour
  {
      public AudioSource source;
      float volume = 0.5f;
  
      void Start()
      {
          source = GetComponent<AudioSource> ();
  
          source.volume = PlayerPrefs.GetFloat ("SliderBackground", source.volume);
      }
      void SaveSliderValue()
      {
          PlayerPrefs.SetFloat ("SliderBackground", volume);
      }
  }

I tried linking the script to the Audio source gameobject and then the Slider gameobject but neither work as intended. Any help on this would be greatly appreciated.

Comment
Add comment · Show 2
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 Adamcbrz · Apr 07, 2017 at 10:07 PM 0
Share

Why are you requiring two AudioSources? That means that the GetComponent might be accessing the wrong AudioSource.

Also how does SaveSliderValue get called? I am assu$$anonymous$$g the slider changes it on value changed. Currently you are never changing the volume variable so you would be saving it to .5f every time. You probably want to crate a method that receives a float and sets the volume.

 void SaveSliderValue(float newVolume)
 {
    volume = newVolume;
    PlayerPrefs.SetFloat("SliderBackground", volume);
 }
avatar image Smithy5995 · Apr 08, 2017 at 11:43 AM 0
Share

I'll give that a go, cheers.
I pretty much just read through different posts and one of contained that code and said it would work so I thought I'd give it a try.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ethqnit · Apr 07, 2019 at 07:08 PM

I was able to fix this by doing the following:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class VolumeValueChange : MonoBehaviour
 {
 
     // Reference to Audio Source component
     public AudioSource audioSrc;
 
     // Music volume variable that will be modified
     // by dragging slider knob
     public float musicVolume = 1f;
 
     void Awake()
     {
         musicVolume = PlayerPrefs.GetFloat("Volume", musicVolume);
     }
     // Use this for initialization
     void Start()
     {
         // Assign Audio Source component to control it
         audioSrc = GetComponent<AudioSource>();
         musicVolume = PlayerPrefs.GetFloat("Volume", musicVolume);
     }
 
     // Update is called once per frame
     void Update()
     {
         // Setting volume option of Audio Source to be equal to musicVolume
         audioSrc.volume = musicVolume;
     }
 
     // Method that is called by slider game object
     // This method takes vol value passed by slider
     // and sets it as musicValue
     public void SetVolume(float vol)
     {
         musicVolume = vol;
         PlayerPrefs.SetFloat("Volume", musicVolume);
     }
 }
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

110 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

Related Questions

How Do I Make A Change A Volume With A Slider? 3 Answers

Adjusting Audio Volume via UI Slider 0 Answers

AudioSource not responding to volume change 0 Answers

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

Not sure how to word this question but having issue with mixer and sound clips 1 Answer


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