Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by basseytje · Dec 21, 2014 at 08:37 PM · playerprefs

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");
     }   
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image basseytje · Dec 22, 2014 at 01:21 AM 0
Share

in the sliders action

avatar image ashleyjlive · Dec 22, 2014 at 03:28 AM 0
Share

@basseytje yeah sorry totally forgot, silly me. I have answered your question below and tell me if it works. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kazishadab · Mar 27, 2017 at 04:26 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges