Unity send value to UI Slider and update Slider bar

25,991

Solution 1

Are you using the new Unity UI?

Using UnityEngine.UI;

Then you can use a public field to access the Slider

public Slider mSlider;

using Gameobject.Find is very slow so it is best to avoid that whenever possible. You can then assign your slider gameobject to this variable in the Editor.

Then in your Start or Update you can assign the value of the slider like this:

mSlider.value = 0.5f;

and the maximum value of your slider like this:

mSlider.maxValue = 1.0f;

Since the slider value is being set to half of the max value, your slider should be in the middle

Solution 2

If you can get the slider using GameObject.Find, you can set its normalizedValue property to update its position.

Note that GameObject.Find doesn't work on objects that aren't active in the scene, so you may have to grab it when the game starts and assign it to a variable which you can refer to later.

Share:
25,991
Silent Ace
Author by

Silent Ace

Updated on January 15, 2020

Comments

  • Silent Ace
    Silent Ace over 4 years

    I am pretty new to Unity and was wondering if you could answer this question. I know how i can get the value from the slider both via the script and through a function set in On Value Changed. However is it possible to send back a value to the slider and update the slider bar position in Unity?