Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using a slider to change an object's transform

Discussion in 'UGUI & TextMesh Pro' started by shaneburger, Oct 19, 2015.

  1. shaneburger

    shaneburger

    Joined:
    Oct 10, 2014
    Posts:
    15
    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?
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    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:

    Code (CSharp):
    1. public void SetPositionX(float val){
    2.     Vector3 pos = transform.position;
    3.     pos.x = val;
    4.     transform.position = pos;
    5. }
     
  3. shaneburger

    shaneburger

    Joined:
    Oct 10, 2014
    Posts:
    15
    So I would just add that script to the object, and then the property SetPositionX would be exposed as a function?
     
  4. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    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. ;)

    Good luck!
     
    shaneburger likes this.