Question by
Broccoliguy04 · Mar 30, 2018 at 01:15 AM ·
slider
Slider value help
So I am trying to change the value of a slider via script, but obviously, it's not working. In fact, it is only working for one of my three sliders. When I load the scene, only one of my sliders is set, while the other two remain at zero. Here are the associated scripts. If you need any more info, feel free to ask, and thank you in advance.
GameControl.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//for the music handeler, Singleton
public class GameControl : MonoBehaviour {
static public GameControl me;
public AudioSource source;
void Start () {
source.volume = SavingLoading.hi.MusicVolume;
if (me == null) {
DontDestroyOnLoad (gameObject);
me = this;
} else if (me != null) {
Destroy (gameObject);
source = GetComponent<AudioSource> ();
}
}
public void AdjustMasterVolume(Slider Mslider,Slider MuSlider, Slider SFXSlider){
SavingLoading.hi.MasterVolume = Mslider.value;
SavingLoading.hi.MusicVolume = MuSlider.value;
SavingLoading.hi.SFXVolume = SFXSlider.value;
source.volume = SavingLoading.hi.MusicVolume*SavingLoading.hi.MasterVolume*1.2f;
}
public void AdjustMusicVolume(Slider slider){
SavingLoading.hi.MusicVolume = slider.value;
source.volume = SavingLoading.hi.MusicVolume*SavingLoading.hi.MasterVolume*1.2f;
}
public void AdjustSFXVolume(Slider slider){
SavingLoading.hi.SFXVolume = slider.value;
}
}
OptionsMenu.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OptionsMenu : MonoBehaviour {
public Slider Master;
public Slider Music;
public Slider SFX;
public AudioClip ping;
public AudioSource source;
void Start(){
SavingLoading.hi.LoadVolume ();
source = this.GetComponent<AudioSource> ();
SetSliders ();
}
public void AdjustMasterVolume(){
GameControl.me.AdjustMasterVolume (Master,Music,SFX);
}
public void AdjustMusicVolume(){
GameControl.me.AdjustMusicVolume (Music);
print (Music.value+" Music");
}
public void AdjustSFXVolume(){
GameControl.me.AdjustSFXVolume (SFX);
source.volume = SavingLoading.hi.SFXVolume*SavingLoading.hi.MasterVolume;
source.Play();
print (SFX.value + " SFX");
}
public void SaveVolume(){
SavingLoading.hi.SaveVolume ();
}
void SetSliders(){
Master.value = SavingLoading.hi.MasterVolume;
Music.value = SavingLoading.hi.MusicVolume;
SFX.value = SavingLoading.hi.SFXVolume;
print (SavingLoading.hi.MusicVolume+" SL.h.MV");
print (SavingLoading.hi.MasterVolume+" SL.h.M");
print (Music.value+" Music");
print (SFX.value + " SFX");
}
public void DeleteHS(){
SavingLoading.hi.DeleteHS ();
}
public void DeleteAllData(){
SavingLoading.hi.DeleteAllData ();
}
}
Part of PlayerController.cs
public class PlayerController : MonoBehaviour {
float speed;
public Text countText;
public Text winText;
public AudioClip winSound;
public AudioClip pingSound;
public float colNum;
private Rigidbody rb;
private int count;
private AudioSource source;
private AudioSource ping;
//from here to next comment, controls the player
void Start (){
if (gameObject.CompareTag ("Lvl1")) {
SavingLoading.hi.currentLevel = 1;
} else if (gameObject.CompareTag ("Lvl2")) {
SavingLoading.hi.currentLevel = 2;
} else if (gameObject.CompareTag ("Lvl3")) {
SavingLoading.hi.currentLevel = 3;
}
SavingLoading.hi.SaveLvl ();
print (SavingLoading.hi.currentLevel);
speed = 10;
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
ping = GetComponent<AudioSource > ();
source = GetComponent<AudioSource> ();
source.volume = SavingLoading.hi.SFXVolume*SavingLoading.hi.MasterVolume*1.8f;
ping.volume = SavingLoading.hi.SFXVolume * SavingLoading.hi.MasterVolume*1.8f;
}
Part of SavigLoading.cs
public void SaveVolume(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/vol.data");
Data vol = new Data ();
vol.MasterVolume = MasterVolume;
vol.MusicVolume = MusicVolume;
vol.SFXVolume = SFXVolume;
bf.Serialize (file, vol);
file.Close ();
print ("Saved vol!");
}
public void LoadVolume(){
if (File.Exists (Application.persistentDataPath + "/vol.data")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/vol.data", FileMode.Open);
Data vol = (Data)bf.Deserialize (file);
MasterVolume = vol.MasterVolume;
MusicVolume = vol.MusicVolume;
SFXVolume = vol.SFXVolume;
file.Close ();
print ("Loaded");
}
Once again, thank you in advance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612155642im_/https://answers.unity.com/themes/thub/images/avi.jpg)