# Object shrinks at same rate no matter what

I am using the following code to shrink a sprite:

public bool shrinking = false;
public float targetScale = 0.1f;
public float shrinkSpeed = 0.1f;

void Update () {
if(shrinking) {
mySprite.transform.localScale -= Vector3.one * Time.deltaTime * shrinkSpeed;
if(mySprite.transform.localScale.x <= targetScale) {
shrinking = false;
}
}
}


However, no matter what I change shrinkSpeed to, the object shrinks at the same rate. I know it probably has something to do with putting it in the update function but where should I move it to fix the problem?

## 2 Answers

It turns out I should have paid more attention in CS class. I didn't realize Unity overrides any initialized values from scripts with whatever is present in the Inspector. Having made all my variables public, the Inspector was overriding them every time, even when I used [HideInInspector]. Simply making all the variables private (which they should have been from the beginning) solved the problem.

You may want to consider setting up your transitions based on a time period instead of a multiplier. That way, you can instead adjust the "Duration" of your transition in milliseconds instead of fiddling with multiplier values.

Consider something like this:

float Duration = 0.5f;
const float DURATION = 0.5f;
Vector3 StartSize = Vector3.one;
Vector3 EndSize = Vector3.zero;
bool Done;

Update(){
if (!Done)
{
Duration -= Time.deltaTime;
if (Duration <= 0) {
Duration = 0;
Done = true;
}
mySprite.transform.localScale = Vector3.Lerp (EndSize, StartSize, Duration / DURATION);
}
}