Using a slider to change an object's transform

I would like to use a slider in the UI to change the transform of an object - either the rotation or location.

It appears that those values are not exposed in a way that I can select them when setting the “On Value Changed” - I select the object, but don’t know what to pick within.

Any idea? Or do I need to expose those properties with a script? If so, how do I do that?

Events like “On Value Changed” can only call functions, not set variables or properties directly. The easiest way would therefore be to just create a simple function, like so:

public void SetPositionX(float val){
    Vector3 pos = transform.position;
    pos.x = val;
    transform.position = pos;
}

So I would just add that script to the object, and then the property SetPositionX would be exposed as a function?

Yes, if you add that as a script called “TransformSetters.cs” for example, you can find “TransformSetters > SetPositionX” in the Inspector’s dropdown.

Some quick terminology though: A property in C# is a member variable that has a getter and setter defined, like this for instance: public string name {get; private set;}

Not a big deal, but things like that could lead to confusion. :wink:

Good luck!

1 Like