- Home /
Getting a slider value from a different scene
I'm just wondering why this doesnt work!? There arent any errors. I want to take the value (from my "menu" scene's slider, and use it's value to plug into as the float in the "MouseLook" Script. I saved my slider as a prefabs like I dragged it into the project area and then switched to my "game" scene and dragged it into the public Slider "slider" box in the inspector. I have a save script on my slider which saves its value, and if I run and reopen the game, the slider values are saved and remain how I set them. However, in my MouseLook script, the sider.value is not effecting the sensitivity! Here are my MouseLook Scirpt, and my slider save script, and it's important to note on the FPSController, the MouseLook script is accessed. If you have used the standard unity asset you would know what im talking about, there is a line of code in the FirstPersonController Scirpt that looks like this: `
[SerializeField] public MouseLook m_MouseLook;
My MouseLook Script(not all of the scirpt, just the top where I am working):
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class MouseLook
{
public float XSensitivity;
public float YSensitivity;
public bool clampVerticalRotation = true;
public float MinimumX = -90F;
public float MaximumX = 90F;
public bool smooth;
public float smoothTime = 5f;
public bool lockCursor = true;
public Slider slider;
public Slider slider1;
private Quaternion m_CharacterTargetRot;
private Quaternion m_CameraTargetRot;
private bool m_cursorIsLocked = true;
public MouseLook mouseLookCustom;
public void sensValue()
{
XSensitivity = PlayerPrefs.GetFloat("OptionScore");
YSensitivity = slider1.value = PlayerPrefs.GetFloat("OptionLives");
}
And lastly, my script to save the values (it is on a seperate game object (empty) on the same scene as the slider ("menu") and has the script attached to it and in the public Slider XSlider area, the slider was dragged directly from the Hierarchy into the box. Thanks for any help!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class sliderSaves : MonoBehaviour { public Slider XSlider;
// Use this for initialization
void Start()
{
XSlider.value = PlayerPrefs.GetFloat("OptionScore");
}
void Update()
{
PlayerPrefs.SetFloat("OptionScore", XSlider.value);
}
}
Answer by Brogan89 · Jun 15, 2017 at 10:57 PM
You probably shouldn't use PlayerPrefs in update function. Also, I'm not 100% sure what you are meaning, but if you want to store and save values across scenes, it is best to use and static class or static variables.
I don't know enough about PlayerPrefs but I never use them, never found a need to. What i would do if I was you if make a class
public static class SaveStuff { public static float optionScore; }
Then from any script you can use go:
SaveStuff.optionScore = XSlider.value; // saving data
XSlider.value = SaveStuff.optionScore; // load stuff
Your answer
Follow this Question
Related Questions
Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers
Slider value not working? 0 Answers
Slider value saving? 1 Answer
Slider issues 1 Answer
Slider.value won't change to INT 1 Answer