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

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.

alt text

using UnityEngine.UI;

did you import it?

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
}