- Home /
Unity 4.6: 2 UI sliders updating will not work
I've got this problem with the UI sliders. i use 2 of them for sound en music volume and i wrote this script. when the scene starts the script gets the playerprefs values en set them to the slider. but in the function: SetSliders(), The function only excecutes the first line of the 2. i even tried switching them. only the first line.
using UnityEngine;
using UnityEngine.UI;
public class OptionsSoundScript : MonoBehaviour
{
public Slider soundSlider;
public Slider musicSlider;
void Start ()
{
SetSliders();
}
//mute a value
public void Mute(string name)
{
PlayerPrefs.SetFloat(name, 0);
SetSliders();
}
//get the value of the slider en store in playerprefs
public void GetSliders()
{
PlayerPrefs.SetFloat("MusicVolume", musicSlider.value);
PlayerPrefs.SetFloat("SoundVolume", soundSlider.value);
}
//get the value of the playerpref and set the value of the slider
void SetSliders()
{
soundSlider.value = PlayerPrefs.GetFloat("SoundVolume");
musicSlider.value = PlayerPrefs.GetFloat("MusicVolume");
}
}
@basseytje yeah sorry totally forgot, silly me. I have answered your question below and tell me if it works. :)
Answer by basseytje · Dec 22, 2014 at 07:21 PM
I fixed it myself, a bit dump i didn't tested this way. Changed the code for the OnValueChange trigger and in the UpdateSlider function i broke the code down in 2 steps and now it works.
using UnityEngine;
using UnityEngine.UI;
public class OptionsSoundScript : MonoBehaviour
{
public Slider soundSlider;
public Slider musicSlider;
void Start ()
{
UpdateSliders();
}
//mute a value
public void Mute(string name)
{
PlayerPrefs.SetFloat(name, 0);
}
//set the sound volume through slider
public void SetSound(float a)
{
PlayerPrefs.SetFloat("SoundVolume", a);
}
//set the music volume through slider
public void SetMusic(float a)
{
PlayerPrefs.SetFloat("MusicVolume", a);
}
void UpdateSliders()
{
float soundValue = PlayerPrefs.GetFloat("SoundVolume");
float musicValue = PlayerPrefs.GetFloat("MusicVolume");
soundSlider.value = soundValue;
musicSlider.value = musicValue;
}
}
Answer by ashleyjlive · Dec 22, 2014 at 01:34 AM
I tried this and like you only noticed the first ui Element being executed. After writing an edited version of your script. I realised that Unity actually got into some sort of loop for some reason, preventing the next line from executing.
To fix this, in the unity editor. Click on the UI element, click Add Component, type "Event Trigger" and add an Event Trigger. Then click "Add New Event Type", then add "PointerUp" and use that to execute the script rather than OnValueChanged trigger.
Btw here's my edited script :) - Nothing is really changed significantly.
public class OptionsSoundScript : MonoBehaviour {
public UIElements uiElements = new UIElements();
[System.Serializable]
public class UIElements
{
public Slider soundSlider;
public Slider musicSlider;
}
void Awake ()
{
SetSliders();
}
public void Mute (string name)
{
PlayerPrefs.SetFloat(name, 0);
SavePlayerPrefs();
SetSliders();
}
public void GetSliders ()
{
PlayerPrefs.SetFloat("MusicVolume", uiElements.musicSlider.value);
PlayerPrefs.SetFloat("SoundVolume", uiElements.soundSlider.value);
SavePlayerPrefs();
}
public void SetSliders ()
{
uiElements.soundSlider.value = PlayerPrefs.GetFloat("SoundVolume");
uiElements.musicSlider.value = PlayerPrefs.GetFloat("MusicVolume");
}
public void SavePlayerPrefs()
{
PlayerPrefs.Save();
}
}
I am also facing the same Issue. The slider gets stucked in the loop i.e slider value changes from 0 to 100 and repeats again and again. Here is my code using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class fullScreenControl : $$anonymous$$onoBehaviour {
public $$anonymous$$ediaPlayerCtrlFullScreen scr$$anonymous$$edia;
public bool m_bFinish = false;
public Slider videoSlider;
protected string video;
private float slidersValue,recentValue;
private bool sliderClick=false;
public GUISkin shadabGUISkin;
private int fingersOnScreen;
public Sprite playImage;
public Sprite pauseImage;
public Button yourImage;
private int i=1;
// Use this for initialization
void Start () {
video = anim2dscript.getVideoName ();
scr$$anonymous$$edia.OnEnd += OnEnd;
scr$$anonymous$$edia.Load (video);
scr$$anonymous$$edia.Play ();
videoSlider.$$anonymous$$Value = 0;
videoSlider.maxValue = scr$$anonymous$$edia.GetDuration ();
//scr$$anonymous$$edia.Play ();
}
void Update(){
videoSlider.maxValue=(float)scr$$anonymous$$edia.GetDuration();
slidersValue=(float)scr$$anonymous$$edia.GetSeekPosition ();
updateSliderPos ();
}
void OnGUI(){
GUI.Label (new Rect (100, 100, 300, 200), "sliderValue: " + videoSlider.value.ToString ());
GUI.Label (new Rect (100, 200, 300, 200), "seekPosition: " + ((float)scr$$anonymous$$edia.GetSeekPosition()).ToString ());
GUI.Label (new Rect (100, 300, 300, 200), "maxvalue: " + videoSlider.maxValue.ToString ());
GUI.Label (new Rect (100, 400, 300, 200), "sliderValue: " + slidersValue.ToString ());
}
public void updateSliderPos(){
videoSlider.value = slidersValue;
}
public void SliderValue(float newValue){
scr$$anonymous$$edia.SeekTo((int)((videoSlider.value)));
}
public void playClicked(){
$$anonymous$$ediaPlayerCtrlFullScreen.$$anonymous$$EDIAPLAYER_STATE state = scr$$anonymous$$edia.GetCurrentState ();
if (state==$$anonymous$$ediaPlayerCtrlFullScreen.$$anonymous$$EDIAPLAYER_STAT$$anonymous$$PLAYING){
yourImage.GetComponent<Image> ().sprite = playImage;
scr$$anonymous$$edia.Pause ();
i = 2;
}else if(state==$$anonymous$$ediaPlayerCtrlFullScreen.$$anonymous$$EDIAPLAYER_STAT$$anonymous$$PAUSED) {
yourImage.GetComponent<Image> ().sprite = pauseImage;
scr$$anonymous$$edia.Play ();
i = 2;
}
}
public void pauseClicked(){
scr$$anonymous$$edia.Pause();
}
void OnEnd()
{
m_bFinish = true;
}
}
Any idea what is the problem? And How to solve it.
Your answer
Follow this Question
Related Questions
Does anyone know why continuously saving to playerprefs would cause a performance drop ? 0 Answers
Playerprefs resetting after quit 2 Answers
Is PlayerPrefs.Save() necessary ? (My game saves without it) 2 Answers
How to save gameobject values with respect to scene? 1 Answer
Mac: PlayerPrefs not deleted 1 Answer