Question by
nullystudio_unity · Jul 14, 2020 at 01:15 PM ·
uiscript.audiosource
how do i get a slider value to stay the same after a scene change?
how do i get a slider value to stay the same after a scene change?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MasterVolumeControl : MonoBehaviour
{
public AudioSource[] soundtrack;
public Slider volumeSlider;
AudioSource audioSource;
private LevelManager theLevelManger;
// Use this for initialization
void Start()
{
audioSource = GetComponent<AudioSource>();
if (!audioSource.playOnAwake)
{
audioSource = soundtrack[Random.Range(0, soundtrack.Length)];
theLevelManger.levelMusic.Play();
}
}
// Update is called once per frame
void Update()
{
if (!audioSource.isPlaying)
{
audioSource = soundtrack[Random.Range(0, soundtrack.Length)];
}
}
void OnEnable()
{
//Register Slider Events
volumeSlider.onValueChanged.AddListener(delegate { changeVolume(volumeSlider.value); });
}
//Called when Slider is moved
void changeVolume(float sliderValue)
{
audioSource.volume = sliderValue;
}
void OnDisable()
{
//Un-Register Slider Events
volumeSlider.onValueChanged.RemoveAllListeners();
}
}
Comment