Smooth look at Random Transform ?

Hello Developers, I stuck on my modified code…

var targetsArray : Transform[];
var damping = 6.0;
var smooth = true;

@script AddComponentMenu("Camera-Control/Smooth Look At")

function Start() 
{

	var target = Random.Range(0, targetsArray.Length);

}

function LateUpdate () {
		if (smooth)
		{
			// Look at and dampen the rotation
			var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
		}
	}

so this is what i modified, I want to look at randomly selected transform but debug log gives me that error :

SmoothLookAtRandom.js(18,78): BCE0005: Unknown identifier: ‘target’.

So what should i do ?

Your variable “target” is local and will work only in Start(), you should declare it outside Start() method like that:

private var target = -1;

function Start() {
   target = Random.Range(0, targetsArray.Length);
}
function LateUpdate(){
   if(smooth && (target != -1)){
   var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}