Scaling up an object if its start Scale (in Inspector) is not (1,1,1)

Cube in scene. Scale sets to f.e. (1.5f, 2f, 1f)

Vector3 lastObjectScale;

void Start()
{
      lastObjectScale = transform.localScale;
      var smallScale = new Vector3(lastObjectScale.x / 100f,
      lastObjectScale.y / 100f, lastObjectScale.z / 100f);
      //or
      //var smallScale = lastObjectScale / 100f;
      transform.localScale = smallScale;
      StartCoroutine(ScaleUp());  
}

IEnumerator ScaleUp()
{
      var scaleSpeed = 0.05f;
      while (transform.localScale.x < lastObjectScale.x + 0.5f)
        {
            var newScaleZ = transform.localScale;          
            newScaleZ += new Vector3(scaleSpeed, scaleSpeed, scaleSpeed);
            transform.localScale = newScaleZ;
            yield return null;
        }
}

It will be the cube.
Why? Why transform.localScale sets to (0,0,0)? And smallScale at Start() became (0,0,0) after dividing, but its component are as expected x=0.015f, y=0.02f, z=0.01).
Help, please. I’m tired and confused.

Did you add some debug messages?

I also noticed in your coroutine you’re setting newScalez = go.transform.localScale and then adding to that, but then you update transform.localScale. Unless go.transform.localScale changes value, your newScalez value will never change as it will always get reset to the same value at the start of your while loop.

I don’t understand your question. Please clearly state what you wanted to happen, what actually happened, and how they are different.

One thing that jumps out at me though is that you are using a variable called “go” on line 19 that is not used anywhere else. I’d guess that either that’s wrong, or the other uses of “transform” are wrong.

Where are you seeing 0,0,0? If you’re seeing it in Debug.Log, Vector3.ToString rounds all three floats. Either provide a custom format to it, or Debug.Log x, y, z individually.

1 Like

From that reply I realized something… so…

IEnumerator ScaleUp()
    {
        var scaleSpeed = 0.05f;
        var scaleVector = transform.localScale * scaleSpeed;
        while (transform.localScale.x < lastObjectScale.x + 0.5f)
        {
            var newScaleZ = transform.localScale;
            newScaleZ += scaleVector;
            transform.localScale = newScaleZ;
            yield return null;
        }
    }

Actually I used scaling wrong… I should to use mult to keep scale factor. Gosh… :sweat_smile: