Search Unity

Set a Specific Object Scale (size) Using a Slider

Discussion in 'Scripting' started by cansub, Mar 2, 2018.

  1. cansub

    cansub

    Joined:
    May 30, 2017
    Posts:
    15
    I am building a character generator and I need to be able to scale the character using a GUI slider. I have no issues with using the GUI but I am having difficulty figuring out how to set a specific scale of the model using the slider. Using the transform.localscale += new vector3(scale, scale, scale); gives me an exponential scale every time the slider is moved (and it is always positive). I want to set a specific scale between 0.8 and 1.2 which will go up and down between 0.8 and 1.2 with the slider. Some help is very much appreciated.
     
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    Don't add it, just set the value
    Code (CSharp):
    1. transform.localScale = new Vector3(scale, scale, scale);
     
  3. cansub

    cansub

    Joined:
    May 30, 2017
    Posts:
    15
    The problem is that as soon as the slider is moved then that value is added to the scale on top of the last frame's value. For instance if during the frame the value was 1.05 and then the next frame (I am moving the slider) the slider value is now 1.1, the 1.1 is compounded onto the scale of the object and it scales up even quicker. If I move the slider backwards, it will keep scaling up but at a slower rate. I need it to actually set the real values of the slider into the scale of the transform of the object.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In the code given, 'scale' would mean the slider's value. It's being assigned, instead of incremented (as was your original post's example). The code provided should work as you intend.
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Then something else is contributing to the scale somehow. If you simply set the localScale based on the slider's value, it should not be possible for scale accumulation to happen.
     
  6. cansub

    cansub

    Joined:
    May 30, 2017
    Posts:
    15
    localScale increases (or decreases) the size of a transform - not sets it. If you enter 0.2 it increases the size by 20 percent, not sets it to 20 percent. From the unity scripting:
    Description
    The scale of the transform relative to the parent.

    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour
    {
    void Example()
    {
    // Widen the object by 0.1
    transform.localScale += new Vector3(0.1F, 0, 0);
    }
    }

    So every time the slider moves and returns a value, the localScale increases the size of the transform by that scale, not setting it to the scale of the returned value. So if the slider returns 1.1 (which is what I actually want set) the scale is actually increase again by 1.1 times. I believe I am using the localScale exactly as intended but it is not what I need. A transform.position sets a position, a localScale does not.
     
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Read that example again. Notice the += operator. Don't use the += operator. Use the = operator.
     
    cansub likes this.
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    No, when you use += you are adding onto the existing scale. Which is why you just use =.

    @BlackPete beat me to it. lol
     
    cansub and BlackPete like this.
  9. cansub

    cansub

    Joined:
    May 30, 2017
    Posts:
    15
    That did it. Thanks gents. I had tried == earlier and it gave an error. = works perfectly.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    == is a comparision check. Does A == B checks if they are the same.
    = sets the value A = B means set the Value of A equal to the value of B.
    += Means to add on and then set the value A += B is similar to A = A + B
     
    cansub likes this.