Can't compare transform.localScale with a float?

Transform.localScale returns a float integer, so how come it gives me an error in this script?

void Update () {
        print (this.transform.localScale);
        if (shrinking) {
            this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
            if (this.transform.localScale < targetScale) {
                shrinking = !shrinking;
            }
        }
    }

Something about being unable to compare Vector3 to a float, but localScale returns a float…

localScale returns a Vector3. Has done for as far back as I can remember.

Oh, I accessed Vector3 with “.x” on the end. Thanks!

void Update () {
        if (shrinking) {
            print ("X " + this.transform.localScale.x);
            this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
            if (this.transform.localScale.x < targetScale) {
                print ("Done!");
                shrinking = false;
            }
        }
    }

I’m still having problems here. It won’t stop shrinking when the localScale is less than the target scale. I even printed out the .x value to see if it was a float and it is.

I have the targetScale set at .6f, and when transform.localScale.x goes below .6 it doesn’t stop shrinking. However, if I swap the < to > in the comparison, it works. So I’m not understanding why it can’t detect the < comparison.

The lerp will shrink the local scale to the targetScale, but it won’t be less than it. If you change the if statement to read <= instead of just < I wouldn’t be surprised if it worked. You may also want to look into Mathf.Approximately and Vector3.Distance. One will tell you the distance between two vector3’s, and the other will tell you when a float is approximately equal to another float.

I’m not quite sure how it operates. Here’s my variable …

targetScale = .6f;

That means I want to shrink it until it’s .6 the original size? Either way, when I print transform.localScale.x it prints out some low number like .32 when the size I wanted it to stop at was .6.

Vector 3 tran = transform.localScale;

float x = tran.x;

Thanks, I got it, but forgot to reply that I had gotten it. I’m curious though, how do I slow the rate of shrinking? No matter what value I change it doesn’t seem to slow it down.

Bump*

simple since ur lerp is doing inside the if statement, therefore, it won’t work

It doesn’t work even if I take it out.

private float targetScale = .2f;
private float shrinkSpeed = .01f;

    void Update () {
        if (go) {
            Vector3 pos = go.transform.position;
            pos.x = this.transform.position.x;
            pos.z = this.transform.position.z;
            go.transform.position = pos;
        }

        if (targetScale >= this.transform.localScale.x && shrinking) {
            print ("Done!");
            shrinking = false;
        }
          
        if (shrinking) {
            print ("Shrinking!");
            this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
        }
            //print ("X " + this.transform.localScale.x);
    }

They all still shrink at the same speed, and never stop shrinking.

quick code for here

    Vector3 newScale;
    void Start()
    {
        newScale = this.transform.localScale;
    }
  
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            newScale = new Vector3(1.5f, 1.5f, 1.5f);
        }
        if(Input.GetKeyDown(KeyCode.B))
        {
            newScale = new Vector3(1.0f, 1.0f, 1.0f);
        }
      
        this.transform.localScale = Vector3.Lerp(this.transform.localScale, newScale, 10 * Time.deltaTime)
    }

I want to shrink it, though. Wouldn’t 1.5 make it grow, or is that not how it works?

this is just a sample, drag the scripts to a cube object and you will understand how lerp work
just show you a way only.
my Lerp is not doing inside the if statement.
But your lerp are

1.0 is default value, 1.5 means scale large

Still not working. Weird thing is I set shrinking to false, but it continues to execute shrinking. If you could, please try running it.

Edit: Got it working with this.

if (shrinking) {
            this.transform.localScale -= Vector3.one * Time.deltaTime * shrinkSpeed;
            print ("Shrinking!" + this.transform.localScale.x);
            if (this.transform.localScale.x < targetScale) {
                print ("Done!");
                shrinking = false;
            }
        }
private Vector3 newScale;  

void Update () {
        if (go) {
            Vector3 pos = go.transform.position;
            pos.x = this.transform.position.x;
            pos.z = this.transform.position.z;
            go.transform.position = pos;
        }

        if (targetScale >= this.transform.position.x && shrinking) {
            print ("Done: " + this.transform.position.x);
            shrinking = false;
        }

        if (shrinking) {
            print ("Shrinking!");
            print ("Shrinking: " + this.transform.position.x);
            this.transform.localScale = Vector3.Lerp (this.transform.localScale, newScale, Time.deltaTime * shrinkSpeed);
        }
            //print ("X " + this.transform.localScale.x);
    }