Scaling an object on collide

Hi all,

I’m trying to create a bumper for a pinball machine that scales up and then down on collision with the ball. I have a script that I think should work, and returns the values I expect, but it doesn’t update the scale of the object. Can anyone see what I’m not doing correctly here?(This is probably an extremely inefficient script, but I’m learning as I go here.)

Thanks in advance!

var initialScale : Vector3;
var tTime:float = 0;

initialScale = transform.localScale;

function OnCollisionEnter(collision : Collision) {
	if (collision.rigidbody) {
			while (tTime <= 1){
				x = Mathf.Lerp(0,2,tTime);
				transform.localScale = initialScale+Vector3(x,0,x);
				tTime += .1*Time.deltaTime;
				//print (transform.localScale.x);
				}
			tTime = 1;
			while (tTime >= 0){
				x = Mathf.Lerp(0,2,tTime);
				transform.localScale = initialScale+Vector3(x,0,x);
				tTime -= .1*Time.deltaTime;
				//print (transform.localScale.x);
				}
			tTime = 0;
			}
	transform.localScale = initialScale;
}

The scaling is something you want to take multiple frames to happen, so you can actually see the effect. This means spreading the effect out over multiple calls to Update(). The way you have implemented it now the scaling up and scaling down happen entirely within one call to Update(). It enters one frame, scales up in the first while loop, scales down in the second while loop, and then ends. All within that one frame. The result being that you don’t see anything happening.

Thanks Tinus.

What would be a good way to step through frames inside the OnCollision() loop?
I tried using the function Update() inside the function OnCollision() but it didn’t seem to like that too much. I also tried adding a yield to the end of each of my scaling loops, and it seemed to work a little better, but still seems a bit wonky.

~M