I’m trying to access Unity 3d 4.6 slider value with a JavaScript for a Volume slider control, I’m not finding any examples anywhere to access the value on the slider, Could some one give me a examples of a JavaScript to access the value on the slider for my Volume slider control or how you would do it using Unity’s new 4.6 UI,
The slider video I looked at on Unity Learn said that the float had to be a Dynamic float like what’s on the camera script they used for an examples,
Also is there any way for Unity 4.6 to access the variables like the function in a dropdown if not it would be nice to have it do so for custom scripts,
Should I be using a different script prefix in JavaScript ( #pragma strict ) for unity 4.6, I noticed C# is using a new script prefix ( usingUnityEngine.UI; ).
Thank you…
I think it’s .value, as in:
var slider = GetComponent<Slider>();
doSomethingWith(slider.value);
Or you could use the OnValueChanged list in inspector when selected on your slider, to pass the value to some script every time it changes, see the example project they released with 4.8 and the script inside called ‘ShowSliderValue’:
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class ShowSliderValue : MonoBehaviour
{
public void UpdateLabel (float value)
{
Text lbl = GetComponent<Text>();
if (lbl != null)
lbl.text = Mathf.RoundToInt (value * 100) + "%";
}
}
Thank you for your reply I’ll Give it a try…
Okay I try the JavaScript and it did not work, I’m looking for a JavaScript to interact with my slider, can someone give another try I need to rely get it working in JavaScript…?
How can I make my variable dynamic so unity 3d 4.6 will accepted it ??
#pragma strict
public var volume1 : float = 1.0;
function Myfunction ()
{
AudioListener.volume = volume1;
}
Hi Ridgerunner,
As you said, slider send a dynamic float value, but your function isn’t receiving any value.
You could try this:
#pragma strict
function Myfunction (value:float)
{
AudioListener.volume = value;
}
The important part is the parameter (value:float) that the function will receive from slider ( dynamic float ).
I use C# I can’t answer about the prefix, but the use of UnityEngine.UI ( using Tarahugger code ) is because it’s using Text class. If you use Slider or any other UI component in your code at least for C# you should include it.
Hope it helps
So is this considered to be a dynamic float and will unity 3d 4.6 recognize it as a dynamic float in the dropdown list( function Myfunction (value:float) )?
Also how would you tie it to a variable
var volume1: float = 1.00;
function Myfunction (value:float)
{
AudioListener.volume = value;
volume1 = value;
this is Just because I hate it when people don’t show the finished product
Here it is, it work and thank you Caio_Lib and all,
//////////////////////////////////////////////
var volume1: float = 1.00;
function Myfunction (value:float)
{
volume1 = AudioListener.volume = value;
}
///////////////////////////////////////////////////////
I was using the solution here but now after starting to use RC1 it does not update anymore. Within the slider OnValueChange suddenly next to the script there is just empty slot with 0 in it and thats the value what is being used within the text.
Hi Cromfeli,
You could try changing your function in OnValueChange to the same function but below the Dynamic float label.
This should work.