How to gradually grow and shrink an object

Alright so I’m pretty new at Unity and actually started recently. Along with Unity I have started learning javascript. So, I have a Unity project in which there is a scene with a single cube. I attempted to make some code with which the cube will gradually grow until it’s x scale reaches 775. After that, it will gradually shrink back to 1. So far I have succeeded in making the cube gradually grow, however, I can’t seem to make the gradual shrinking part work.
Here is my code:

var growthRate: int=1;
public function grow()
{
	transform.localScale.x=transform.localScale.x+growthRate;
	transform.localScale.y=transform.localScale.y+growthRate;
	transform.localScale.z=transform.localScale.z+growthRate;
}

public function shrink()
{
	transform.localScale.x=transform.localScale.x-growthRate;
	transform.localScale.y=transform.localScale.y-growthRate;
	transform.localScale.z=transform.localScale.z-growthRate;
}

public function main()
{
	if(transform.localScale.x!=775)
	{
		grow();
	}
	else
	{
		while(transform.localScale.x!=1)
		{
			shrink();
		}
	}
}


function Update() {
	
	main();
	
}

Earlier, I attempted to put a while and a for loop in the Update function, because I knew this would be the simplest in means of gradually increasing a value. However, that didn’t seem to be the case. So I tried using if statements instead…The gradual growth part worked, but when it reached it’s maximum of 775, it would simply shrink down to 774 and then go back to 775. It looped forever after that. So now, for my shrink function, I’m using a while statement so that the cube will actually shrink down to it’s usual size. However, the only problem with the while loop is that it doesn’t make the shrinking gradual. Instead, the shrinking happens instantly.
How can I fix this?

This logic is faulty:

if(transform.localScale.x!=775)
{
   grow();
}
else
{
   while(transform.localScale.x!=1)
   {
     shrink();
   }
}

It would the scale would increase at the rate of 1/frame. Then if it became equal to 775 it would enter the while loop and head back down to 1 all in on frame.

Given the imprecision of floats it is not guaranteed to actually register with either guard check.

It is usually better to check if a float exceeds the given bounds. For this type of back-and-forth type of behaviour it is also require to somehow remember which way you are going.

Here is one possible implementation:

var scaleRate: float = 1.0f;
var minScale: float = 775.0f;
var maxScale: float = 1.0f;

public function ApplyScaleRate() {
    transform.localScale += Vector3.one * scaleRate;
}
 
public function main() {
    //if we exceed the defined range then correct the sign of scaleRate.
    if(transform.localScale.x < minScale) {
        scaleRate = Mathf.Abs(scaleRate);
    }
    else if(transform.localScale.x > maxScale) {
        scaleRate = -Mathf.Abs(scaleRate);
    }
    ApplyScaleRate();
}
 
function Update() {
    main();
}

Note: I haven’t used it here but you may like to look at Mathf.PingPong when building this type of back-and-forth animation.

transform.localScale.x = .5f;
Error: Cannot modify the return value of ‘Transform.localScale’ because it is not a variable