Hi all, I hope you can help. In my game I really want to have a working volume slider. I can change the volume with the slider and it works great and continues to work while in the next scene, however, when I return to the main menu(That holds the volume slider) I move the slider, but it doesn’t change the sound anymore. I would like to be able to leave the main menu and then return while being able to change the slider.
The code is as follows:
using UnityEngine;
using System.Collections;
public class VolumeSlider : MonoBehaviour
{
// Reference to Audio Source component
private AudioSource audioSrc;
// Music volume variable that will be modified
// by dragging slider knob
private float musicVolume = 1f;
// Use this for initialization
void Start()
{
// Assign Audio Source component to control it
audioSrc = GetComponent<AudioSource>();
}
// 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;
}
}
AND a second script to save the sliders position after exiting the game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderSave : MonoBehaviour
{
public Slider slider;
public float sliderValue;
public void Start()
{
slider.value = PlayerPrefs.GetFloat("save", sliderValue);
}
public void ChangeSlider(float value)
{
sliderValue = value;
PlayerPrefs.SetFloat("save", sliderValue);
}
}
Thank you and I hope you can help!