using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class scaleWithSlider : MonoBehaviour
{
public float scale;
public Slider slider;
void Start()
{
//Cache the Slider and the ParticleSystem variables
slider = GameObject.Find("Slider").GetComponent<Slider>();
}
void OnEnable()
{
//Subscribe to the Slider Click event
slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}
//Will be called when Slider changes
public void sliderCallBack(float value)
{
Debug.Log("Slider Changed: " + value);
scale = value;
}
// Update is called once per frame
private void Update()
{
transform.localScale = new Vector3(scale, scale, scale);
}
}
In the first image you linked, there is in fact a “changeScale” function showing inside of your “scaleWithSlider” script.
So… does that solve this, or did i miss the problem?^^
You’re not using the value of the slider anywhere from what I can see. You’re instead setting the scale of an object to ‘value’ every time sliderCallBack event is called.
Instead of: scale = value
try doing: scale = slider.value
Edit:
Oh and remove the value parameter. You don’t need it.
So it works on the slider now in editor
but not on play mode as you can see:
q17ma
What am I missing?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class scaleWithSlider : MonoBehaviour
{
public float scale;
public Slider slider;
void Start()
{
//Cache the Slider variables
slider = GameObject.Find("Slider").GetComponent<Slider>();
}
void OnEnable()
{
//Subscribe to the Slider Click event
slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}
//Will be called when Slider changes
public void sliderCallBack(float value)
{
Debug.Log("Slider Changed: " + value);
scale = slider.value;
}
// Update is called once per frame
private void Update()
{
transform.localScale = new Vector3(scale, scale, scale);
}
}
Maybe the Start() method is referencing the wrong slider? It could be the case if you have multiple “Slider” objects in your scene, in that case, it would return only the first one. Try removing the Start() method and reference the slider only through the inspector (as you do in Editor I hope). Not sure if it’s going to work, but its something that should be done anyhow and can’t hurt to try.
Thanks @Dextozz
There is only 1 ‘Slider’. But I dragged and dropped the reference anyway and still it didn’t work
I also tried commenting out [ExecuteInEditMode] which didn’t work either.
Is there somewhere else I should post this?
Would love to solve it, thanks