0
\$\begingroup\$

I am new to game dev. I have below code and it`s work good. I can change volume using this code and with AudioSource slider script. I also saving this volume state using playerprefs from 0-1. My problem is I can not keep slider button to its saved value when reload / replay the game.

If slide has value like .72 or anywhere between 0-1. then audio play with perfect saved volume but button not set to .72. Any one can help.

How can i save button position on saved value when start play again.

My code is...

using UnityEngine;
using System.Collections;

public class vfxslider : MonoBehaviour {

void Awake(){
    GetComponent<AudioSource> ().volume = PlayerPrefs.GetFloat ("CurVol");
}
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
//  AudioListener.volume = PlayerPrefs.GetFloat ("CurVol");
}

public void slidervolumecontrol(float volumecontrol){
    GetComponent<AudioSource> ().volume = volumecontrol;
    //audio = GetComponent<AudioSource> ().volume;
    // audio.volume = volumecontrol;
    PlayerPrefs.SetFloat("CurVol",GetComponent<AudioSource> ().volume);
    PlayerPrefs.Save ();
    }
}

In first picture I have sated volume value of first VFX slider to 0.60800. On this I can listen music.

enter image description here

but in second When i stop playing and play again then slide button remain in zero position but still i can hear music.and also I can not see saved value of volume in right side. check that volume is zero but still I can hear music with 0.60800 value.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ do you mean the slider on the AudioSource Component is not being set in the inspector or what ? \$\endgroup\$ – Uri Popov Apr 26 '16 at 11:19
  • \$\begingroup\$ No AudioSource component sated. I can not set slide button at saved volume position. like if volume value = 0.50 then sliders rounded button should set to middle of slider. This is not working. can you tell me how can get and set value of volume to set sliders button.? @UriPopov \$\endgroup\$ – hardwork Apr 26 '16 at 11:27
  • \$\begingroup\$ please show the code that handles setting the button then. Also are you using the UI component names Slider or have you created your own implementation? \$\endgroup\$ – Uri Popov Apr 26 '16 at 11:28
  • \$\begingroup\$ no i don`t have extra code this is code that i am using. I have not any kind of code for button. @UriPopov \$\endgroup\$ – hardwork Apr 26 '16 at 11:33
  • \$\begingroup\$ I'm not understanding you. Please provide a screenshot that shows your problem and I will try to help you. \$\endgroup\$ – Uri Popov Apr 26 '16 at 11:45
1
\$\begingroup\$

I hope this code I wrote helps you. If you have any more questions please ask.

using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {

    private AudioSource src;
    public Slider slider;// This is so we can drag the slider to the inspector view ot this script
    // Use this for initialization
    void Start () {
        src = GetComponent<AudioSource>();// This gets a reference to the Audio source you can get it any way you want
        src.volume = PlayerPrefs.GetFloat("CurVol");// Set the saved volume
        slider.value = src.volume; //This sets the slider value to the same amout as the volume on the audio source.
        slider.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown("a")) // I'm just using the A key as a event trigger for the save.
        {
            PlayerPrefs.SetFloat("CurVol", src.volume); // This will save the volume of the Audio Source not the slider
            // To save the slider value
            //PlayerPrefs.SetFloat("CurVol", slider.value);    
            PlayerPrefs.Save();

        }
    }
    public void ValueChangeCheck()
    {
        src.volume = slider.value;
    }
}
\$\endgroup\$
  • \$\begingroup\$ sorry but i have done this brfor 30 second and work like charm. thanks for help. \$\endgroup\$ – hardwork Apr 26 '16 at 12:04
  • \$\begingroup\$ @hardwork you can still accept the answer so that it's easier to find for other people who might have the same problem. \$\endgroup\$ – Uri Popov Apr 26 '16 at 12:05
  • \$\begingroup\$ I will try your this code with my player animation. thanks again \$\endgroup\$ – hardwork Apr 26 '16 at 12:06
  • \$\begingroup\$ I test your code and get one problem. In your code there is not any dynamic class to apply slider for value change. In my code is` slidervolumecontrol` so i can apply this to slider. but in your code i cant fine it. \$\endgroup\$ – hardwork Apr 26 '16 at 12:10
  • \$\begingroup\$ This is because I did not write that part since you did not have a problem with that. By the looks of your screenshots you have already managed to subscribe a method to the OnValueChanged event of the slider. I added a delegate call in my example code for you to look at. \$\endgroup\$ – Uri Popov Apr 26 '16 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.