- Home /
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value
I am new to Unity and am trying to create a Pong game with some additional features, such as changing the players' speed. The slider I created and player are in different scenes so to go around this I used the following piece of code:
 public class PlayerSpeed : MonoBehaviour {
 
     public Slider PSlider;
     float speed;
 
     public void ChangePlayerSpeed(Slider PSlider){
         speed = PlayerPrefs.GetFloat("Speed", 0);
         PSlider.value = speed;
     }
 }
I am certain my issue is in this bit of code as I can change the slider value to another number in the Inspector, but once I try to slide the value, it changes to the value I assigned in PlayerPrefs.SetFloat. Though maybe the issue is in the other piece of code for the actual movement speed as I then again use PlayerPrefs.GetFloat, rather than the newly assigned speed. public class PlayerControls : MonoBehaviour {
     public KeyCode moveUp = KeyCode.W;
     public KeyCode moveDown = KeyCode.S;
     float speed;
     public float boundY = 2.25f;
     private Rigidbody2D rb2d;
 
 
     void Start () {
         rb2d = GetComponent<Rigidbody2D> ();
         speed = PlayerPrefs.GetFloat("Speed", 0);
     }
 
     void Update () {
         var vel = rb2d.velocity;
         if (Input.GetKey (moveUp)) {
             vel.y = speed;
         } else if (Input.GetKey (moveDown)) {
             vel.y = -speed;
         } else if (!Input.anyKey) {
             vel.y = 0;
         }
 }
Any help will be appreciated, been stuck on trying to get this slider to change the speed for hours.
I don't see anywhere in your code where you SetFloat. Therefore the default of 0 is always there.
Unless you do call it but in another script?
Answer by Michele0 · Nov 28, 2017 at 10:39 PM
Managed to fix it. Removed the script I had created which included PlayerPrefs.SetFloat, instead added this to my already existing script: public class PlayerSpeed : MonoBehaviour {
     public Slider PSlider;
 
     public void ChangePlayerSpeed(Slider PSlider){
         PlayerPrefs.SetFloat("Speed", PSlider.value);
     }
 }
Actually a really easy fix haha.
Your answer
 
 
             Follow this Question
Related Questions
UI Slider, Save Values and Change Them 0 Answers
How to Load PlayerPrefs from a different scene 1 Answer
Multiple Cars not working 1 Answer
Slider moving on hover 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                