You are not allowed to call this Function..??

Hello i got a script that make the camera follow a target(Mouse Orbit with the zoom option added). For the target,i wanted it to be set automatically,so i removed the var target : Transform; , to var target = GameObject.FindGameObjectWithTag(“Boatcamtarget”).transform;

But i got this error:

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.

Here the script

//var target : Transform;

var target = GameObject.FindGameObjectWithTag("Boatcamtarget").transform; 

var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

var distanceMin = 3;
var distanceMax = 15;

private var x = 0.0;
private var y = 0.0;

@script AddComponentMenu("Camera-Control/Boat Mouse Orbit")

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		       
        var rotation = Quaternion.Euler(y, x, 0);
        
        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
       
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        
        transform.rotation = rotation;
        transform.position = position;
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

Anybody know why i got this error??

Thank you!

You can not set a field during declaration with like that due to how the call works. You will need to set it in the Start or Awake function.

var target:Transform; // left unset

// other variables

function Start() {
  target = GameObject.FindGameObjectWithTag("Boatcamtarget").transform;
}

To add a bit of detail, fields can not be set by anything inline that isn’t static. Static ensures that the class member exists. GameObject.FindGameObjectWithTag(“Boatcamtarget”).transform may not exist when your script(class) is constructed because you’re returning and instance of a GameObject(if found) and the instance of it’s transformation object that may not exist.