- Home /
Unity 4.6 UI - set slider value via script
Hi,
I am trying to restore the user's preferences on a mobile game by reading previously saved values on PlayerPrefs (let's call the variable V). When the user goes to the Settings menu, I would like to set the volume slider to V. - I know V is in the range (0, 1).
I am attempting the following, but the value is not changing, as if the line was ignored:
GameObject.Find ("sliderSensitivity").GetComponent<Slider>().value = PlayerPrefs.GetFloat("sensitivity");
Any clue? Thanks a lot
Answer by Zephrim · Sep 20, 2014 at 04:31 AM
I took a few liberties with your code, but I think it should be a decent fit for what you're trying to do. I recommend you make sure that nothing returns null and erroring if it does.
Also the fact that you cannot find a GameObject if it isn't active in the scene has also messed with me a few times. So if it's the type of object that I plan on hiding quite often, then I'll manually look for it once and store it in an array or dictionary depending on what it is.
The Code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class OptionsMenu : MonoBehaviour
{
/// <summary>
/// The Default Volume Level.
/// </summary>
/// <remarks>
/// This will only get set if the VolumeLevel
/// hasn't been previously saved.
/// </remarks>
public const float DefaultVolumeLevel = 0.5f;
/// <summary>
/// The Volume's Slider
/// </summary>
/// <remarks>
/// Since we plan on using this multiple times, it's better to store it rather then continuously trying to find the component.
/// </remarks>
Slider volumeSlider;
// Use this for initialization
void Start()
{
// Try to find the desired GameObject.
// This will only find active GameObjects in the scene.
GameObject temp = GameObject.Find("Volume Slider");
if (temp != null)
{
// Get the Slider Component
volumeSlider = temp.GetComponent<Slider>();
// If a Slider Component was found on the GameObject.
if (volumeSlider != null)
{
// This is a Conditional Statement.
// Basically if volumeLevel isn't null,
// then it uses it's value,
// otherwise it uses the DefaultVolumeLevel that we've set above.
volumeSlider.normalizedValue = PlayerPrefs.HasKey("VolumeLevel") ? PlayerPrefs.GetFloat("VolumeLevel") : DefaultVolumeLevel;
}
else
{
Debug.LogError("[" + temp.name + "] - Does not contain a Slider Component!");
}
}
else
{
Debug.LogError("Could not find an active GameObject named Volume Slider!");
}
}
/// <summary>
/// Called when the player clicks the Apply Button, this saves all of the values.
/// </summary>
public void OnApply()
{
// Set the VolumeLevel float.
PlayerPrefs.SetFloat("VolumeLevel", volumeSlider.normalizedValue);
}
}
Just a picture of my Unity Instance to show what I had.
Thanks again Zephrim, you were right about an error interfering with the results. Fixed it.
Answer by gregroberts · Dec 23, 2014 at 04:56 AM
Here's the solution, in JavaScript: Be VERY careful with the syntax, its exxtremely case sensitive.
import UnityEngine.UI;
var mySlider: UnityEngine.UI.Slider;
function Start () {
// substitute 'sliderName' with the literal name
// of your slider as it appears in the hierarchy:
mySlider = GameObject.Find("sliderName").GetComponent(UnityEngine.UI.Slider);
}
function Update () {
mySlider.value = n;
// insert desired variable here in place of 'n'
// slider will automagically adjust to display new value
}
@LexGear It is like using UnityEngine.UI; in C# it basically imports all the functions and classes that belongs to the UI Namespace into the current script so that we can work with the Unity's new UI system in the current script in which it is imported. It's pretty basic I would recommend you to go through some C# tutorials to get familiar with the namespace concept
Your answer
Follow this Question
Related Questions
Unity 4.6 Change the content from a text Object through script 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity 4.6 UI - change image's source image via script 4 Answers
Is it faster to set a variable every frame or check if it needs to be changed? 3 Answers
Application.OpenURL not working 1 Answer