- Home /
Change the value of a slider when pressing a UI Button
Using C sharp, how would one go about changing the value of a slider set from 0-0.75 using a UI button. Using increments of 0.1 each time the button is pressed. I have been battling this question for 2 days trying to understand how a button and a slider can interact with each other. So, any ideas are very much appreciated!
Answer by Akash-bhatt · Feb 04, 2017 at 03:00 PM
Add ButtonClick Event in Button
void OnButtonClicked(){ slider.value += 0.1f; }
make sure your sliders min value is 0 and max value is 0.75
Answer by JeevanjotSingh · Feb 04, 2017 at 02:00 PM
Use UnityEngine.UI Name space
Then Create Slider Variable
public Slider ui;
Assign Script to ui button ,Drag and Drop your Slider object from canvas to that public field inside script (Hoping that you know basic concepts of unity as well)
Then Create Button variable like you did for Slider
public Button yourButton;
and Create Click Event. If don't know then go here https://docs.unity3d.com/ScriptReference/UI.Button-onClick.htmlAnd instead for Printing something just increment slider.value with desired value . https://docs.unity3d.com/ScriptReference/UI.Slider-value.html
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Slider to modify a countdown time in another scene 0 Answers