error: You are not allowed to call this function...

Hi there, Im a Uniy and scripting noob. I have just made a switch that moves a wall and its all working, BUT Im getting this error in the console still:

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.

This is my code

private var enterSwitch = false;
private var wall; //this is the line it is complaining about
wall = GameObject.Find ("Wall_01");  //finds the wall game object

function Update ()
{
	if (enterSwitch == true  Input.GetKeyDown("k")){
		Debug.Log("pulled lever");
		wall.transform.localPosition = Vector3(-600, 200, 300); //moves the wall
		enterSwitch = false; //de-activates the switch
		}
}

//trigger volume that detects if the player can use the switch
function OnTriggerStay(hit : Collider)
{
    if(hit.gameObject.tag == "Player"){
		enterSwitch = true;
    }
}

Im dont really understand what unity is complaining about, especially since the code does what I want it to.

I have found that Unity does this when Using certain thing… I can predict when it is going to do it but I just can’t put it into words how to explain it…

Anyways, to get rid of the error just declare the variable and don’t call it. Then call it in your Start function. It will give you exactly the same effect but will not cause the error message.

Remember, if you compile a script and it works and you then modify the script and get an error, when you run the game, Unity will use the previous version of your script so it may look like it is working but it isn’t…

Try this:

function Start()
{
Debug.Log("Error 1");
}

Add this to any object you currently have in the game. Run the game and notice the message you get…

Now replace that code with this:

function Start()
{
GameObject = Transform.heightmap.3rdPixel;
Debug.Log("You will never see this");
}

Now try running the game again…

this happens when you declare a variable and at the same time you assign a value to it.
move the assign part into a function ( Awake()) and the error will be gone.

More like:
When assigning a value from a function while declaring the variable

//this is fine...
var a: int = 10;

//this isn't
var a: int = Ten();

function Ten() : int
{
return 10;
}