Slider to scale object transform scale?

Hi
Spent over 3 hours on this now so feeling very stupid :confused:
Trying to get a slider to drive an objects scale at runtime… Anyone know the solution?

First I tried brackey’s tutorial

but it wouldn’t show Dynamic method. Just get this image:


Code

Then I found this c# - No dynamic float visible for attachment in unity - Stack Overflow but it’s not working - see video - scales in editor but not in play.
nq12i

Code is:

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);
    }

}

Any ideas how I solve?

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.

Thanks @Dextozz !

So it works on the slider now in editor :slight_smile:
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);
    }

}

Try this

void OnEnable()
    {
        //Subscribe to the Slider Click event
        slider.onValueChanged.AddListener(delegate { sliderCallBack(slider); });
    }
    //Will be called when Slider changes
    public void sliderCallBack(Slider slider)
    {
        Debug.Log("Slider Changed: " + slider.value);
        scale = slider.value;
    }
1 Like

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 :confused:
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

Your start function will overwrite the reference in the editor. Just try removing it.

There’s really no other place to post this, you could try creating a new post if you want to start with fewer replies though.