- Home /
Pass in a reference directly into a script so that it update the appropriate variable value when the slider is moved.
I was wondering if there is a way to pass in a reference directly into a script so that it update the appropriate variable value when the slider is moved. I need to use a lot of slider to change scripts floats. This two methods works but it's dirty to code a function for each slider, so i look for a generic approach.
//Molecule Script
public static float generateThreshold= 1.0f;
//GUI Script
public Slider treshold;
public void SliderUpdate (){
Molecule.generateThreshold = treshold.value;
}
//OR
public void SliderUpdate (float val){
Molecule.generateThreshold = val;
}
By the way i tried to use the generic {get; set;} methods it's work fine with dynamic float but mine are statics. So i thought about using ref key word (https://msdn.microsoft.com/en-us/library/14akc2c7.aspx) But onValueChanged take float argument so my code below don't work but it's the kind of method i want.
void Start(){
treshold.onValueChanged.AddListener (SliderUpdate (ref test,treshold));
}
public void SliderUpdate (ref float floatToUpdate,Slider sliderToLink){
floatToUpdate = sliderToLink.value;
}
Thanks
Please excuse my poor English
if you're using the same script for multiple sliders then don't use static variables - get a reference to each instance to access each of them...
i'm working on someone's else code these variables are static (and must stay static for some reasons) and i try to link them easily to slider without coding one specific function for each slider.
if each slider shares the same static variable, changing in one place will change all of them. the reference which you asked about is required, but the class variable cannot be static to use it in the way you need.
why must they remain static?
ok i think i didn't explain my issue correctly i don't want to edit the same static variable. i have various variable i want to edit their value using slider and i'm look for a way to write a method which given a slider and static variable will edit the variable value when the slider is modified. I can write a function for each of these value but i was trying to figure out a better way. $$anonymous$$aybe i'm missing something i'm new to unity and c#.
//$$anonymous$$y $$anonymous$$olecule script
public static float variable1 = 1.0f; // static variables cause they are visuals settings
public static float variable2 = 1.0f;
public static float variable3 = 1.0f;
Your answer
Follow this Question
Related Questions
UI Slider; -x to +x range, 0 center; how can I get it to fill both directions starting from center? 3 Answers
UI slider "End slide" event 3 Answers
Use Slider for Geometry (GameObject) 'layers' 1 Answer
How to get an object to follow the Slider knob? 0 Answers
How to add sound to the movement of the radial slider 0 Answers