Javascript - assigning a function to a variable error

Hi there,

I followed a tutorial series to get myself kick-started with unity, and now i’m going it alone for the first time. In the tutorials I followed, as far as I understood, he created a collision function, then assigned that to a variable so that the data can be accessed outside of the function.

In my own project, I wanted to use the same principals. I created a character that moves dependent on a velocity, and now im trying to use collision detection to get the camera to track the player when they are close to moving out of the bounds of the screen. Whenever I try to run the program it comes up with the error:

“UnassignedReferenceException: The variable playerCol of ‘playerScript’ has not been assigned.
You probably need to assign the playerCol variable of the playerScript script in the inspector.
playerScript.Update () (at Assets/playerScript.js:76)”

As far as I understand, in the tutorial I didn’t need to assign the variable in the inspector, since it was being assigned in the code. I even compared it to the other code and I can’t work out what the issue is…

Here is the code i’m using:

var ySpeed : float = 0;
var yVelocity : float = 0.01;
var xSpeed : float = 0;
var xVelocity : float = 0.01;
var maxSpeed : float = 5;
var playerCol : GameObject;

function Start () {

}

function Update () {

	//if you push up, move up
	if(Input.GetAxis("Vertical") > 0  ySpeed < maxSpeed){
		ySpeed = (ySpeed + yVelocity);
	}

	//if you push up and are going too fast, slow down
	if(Input.GetAxis("Vertical") > 0  ySpeed > maxSpeed ){
		ySpeed = (ySpeed - yVelocity);
	}

	//if you push down, move down
	if(Input.GetAxis("Vertical") < 0  ySpeed > -maxSpeed){
		ySpeed = (ySpeed - yVelocity);
	}

	//if you push down and are going too fast, slow down
	if(Input.GetAxis("Vertical") > 0  ySpeed > -maxSpeed ){
		ySpeed = (ySpeed + yVelocity);
	}

	//if neither up nor down are pressed, slow Y axis
	if(Input.GetAxis("Vertical") == 0 ){
		if (ySpeed > 0){
			ySpeed = (ySpeed - yVelocity);
		}
		if (ySpeed < 0){
			ySpeed = (ySpeed + yVelocity);		
		}
	}

	//if right is pressed, go right
	if(Input.GetAxis("Horizontal") > 0  xSpeed < maxSpeed){
		xSpeed = (xSpeed + xVelocity);
	}

	//if you push right and are going too fast, slow down
	if(Input.GetAxis("Horizontal") > 0  xSpeed > maxSpeed ){
		xSpeed = (xSpeed - xVelocity);
	}

	//if left is pressed, go left
	if(Input.GetAxis("Horizontal") < 0  xSpeed > -maxSpeed){
		xSpeed = (xSpeed - xVelocity);
	}

	//if you push up and are going too fast, slow down
	if(Input.GetAxis("Horizontal") > 0  xSpeed > -maxSpeed ){
		xSpeed = (xSpeed + xVelocity);
	}

	//if left nor right are pressed, slow x axis
	if(Input.GetAxis("Horizontal") == 0 ){
		if (xSpeed > 0){
			xSpeed = (xSpeed - xVelocity);
		}
		if (xSpeed < 0){
			xSpeed = (xSpeed + xVelocity);		
		}
	}

	if (playerCol.tag == "vertical"){
		gameObject.FindWithTag("camera").transform.Translate(Vector3 (0, 1 * ySpeed * Time.deltaTime,0));
	}

	else {
		transform.Translate(Vector3 (0, 1 * ySpeed * Time.deltaTime,0));
	}

	if (playerCol.tag == "horizontal"){
		gameObject.FindWithTag("camera").transform.Translate(Vector3 (1 * xSpeed * Time.deltaTime,0,0));
	}

	else {
		transform.Translate(Vector3 (1 * xSpeed * Time.deltaTime,0,0));

	}
}


function OnCollisionEnter(col : Collision){
	playerCol = col.gameObject;
}

thank you!

Update is called each frame, there isn’t a guarantee that playerCol was ever assigned, best to check if it’s null first, you may also want to declare it as null. According to your code, playerCol isn’t assigned until a collection is entered.

var playerCol : GameObject = null;

//... 
    if (playerCol  playerCol.tag == "vertical"){

        gameObject.FindWithTag("camera").transform.Translate(Vector3 (0, 1 * ySpeed * Time.deltaTime,0));

    }
    else {

        transform.Translate(Vector3 (0, 1 * ySpeed * Time.deltaTime,0));

    }

    if (playerCol   playerCol.tag == "horizontal"){

        gameObject.FindWithTag("camera").transform.Translate(Vector3 (1 * xSpeed * Time.deltaTime,0,0));

    }
    else {

        transform.Translate(Vector3 (1 * xSpeed * Time.deltaTime,0,0));

} 
//...