- Home /
Slider doesn't work on Android device
Hello! I'm new to Unity. I made a slider, who can change the size of an object. The higher the value of the slider is, the bigger the object becomes. It's a very easy function.
public void changeSize(float sliderValue)
{
GameObject.FindWithTag("Robot").transform.localScale = new Vector3(sliderValue, sliderValue, sliderValue);
}
It does work if started in Unity preview, but if I build it as apk and start it on my Android device, the slider does not change the size of my object anymore. I can drag the thumb with my finger to the left and right, but it doesn't change the value of the slider. Does any of you know, what the problem could be?
Answer by Link17x · Mar 15, 2020 at 01:11 PM
Where is this method being called from? Is it really being called every time the slider value changes?
[SerializeField] private GameObject m_Robot;
private Slider m_Slider;
private void Start()
{
m_Slider = GetComponent<Slider>();
m_Slider.OnValueChanged.AddListener(delegate
{
ValueChange();
}
}
public void ValueChange()
{
var value = m_Slider.value;
m_Robot.transform.localScale = new Vector3(value, value, value);
}
This might not solve the problem, but it's better to reference the gameObject rather than use "Find". Also, by adding the listener, it will call the "ValueChange" method itself whenever the value of the slider changes.
Another thing to consider is setting a min and max size. Check the min and max value on your slider too. If it's on the default (min = 0, max = 1) then it might be that your gameObject is there, it's just being scaled way too small.
Make sure your method is being called properly (or set it up how I did in the code above)
Reference the gameObject rather than using "Find", I don't think it's good practice to use. I don't have experience using it, but maybe it's not able to find the right gameObject when you build to mobile?
Check your min/max value on the slider and/or try setting a min size in code. Make sure your gameObject isn't too small to see (especially on mobile where the screen is smaller).
You are right. I referenced the gameObject like you said and used the Listener. Now it's working on my Android device, too. Thank you!
Your answer
Follow this Question
Related Questions
Change position - cameras - slider 1 Answer
Editor Scripting: How To Detect a Change in Player Settings 2 Answers
UI Slider Value Increases Far Too Slowly With Controller. 1 Answer
My mouse sensitivity value don't update in other function than sliderValueChange 0 Answers
using rgb to change color c# 1 Answer