Where can a Lerp be used?

I’m trying to make a lerp for one of my enemies once its trigger is activated. So I have to put it in the function that dictates that, which is a collision function.

Does lerp only work in function Update? Or do I have something wrong?

Here are the relevant parts of my script:

var thePrefab: GameObject;
var enemyActive : boolean = true;
var jelReg : Material;
var jelShock : Material;
var duration = 60.0;

private var coin : GameObject;
private var player : PlayerScript;

function Start(){
player = GameObject.FindWithTag("Player").GetComponent(PlayerScript);
renderer.material = jelReg;
}

function Update () {
}


function OnCollisionEnter(theCollision : Collision){
	if(player.Diamond == true){
		Destroy(gameObject.transform.root.gameObject);
		coin = Instantiate(thePrefab, transform.position, Quaternion.identity);
	}
	if(player.Diamond == false){
		if(theCollision.gameObject.tag == "Player"){
			yield WaitForSeconds(2);
			gameObject.collider.isTrigger = true;
			enemyActive = true;

			var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
			renderer.material.Lerp (jelReg, jelShock, lerp);

			yield WaitForSeconds(3);
			gameObject.collider.isTrigger = false;
			renderer.material = jelReg;
			enemyActive = false;
		}
	}
}

it’s probably something small, or that lerps just can’t be used outside of the update function. Whatever the case, please help!

OnCollisionEnter gets called only, you guessed it, on collision enter. it gets executed once.

lerping is a process over time, so you will need a way to constantly update the lerp over time. An update or fixed update is the perfect place to do this.

have a read here, it describes it a little bit better.

have you tried on collision enter, then on collision stay. [Unity - Scripting API: Collider.OnCollisionStay(Collision)][1]