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?
The Slider is commonly used where a certain value should be set between a maximum and minimum value pair. One of the most common usage of this is for audio volume, or screen brightness. To create a slider, go to Create → UI → Slider. A new Slider element should show up on your scene.
Create the UI Text (user interface) in unitySelect UI and pick the "Text" option. Position it in the right middle holding out + Shift keys change its text field modify its font size set overflow change. Right-click on the canvas and create a new UI slider. Select UI and pick the "Slider" option.
The slider component is a Selectable that controls a fill, a handle, or both. The fill, when used, spans from the minimum value to the current value while the handle, when used, follow the current value. The anchors of the fill and handle RectTransforms are driven by the Slider.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With