- Home /
Unassigned Reference Exception error
I am using the Camera2DFollow script and whenever i press play i get a thousand errors saying :
UnassignedReferenceException: The variable target of Camera2DFollow has not been assigned. You probably need to assign the target variable of the Camera2DFollow script in the inspector. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineTransform.cs:28) Camera2DFollow.Update () (at Assets/Sample Assets/2D/Scripts/Camera2DFollow.cs:29)
The Camera2DFollow script is as follows:
using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
Vector3 ifDead;
// Use this for initialization
void Start () {
lastTargetPosition = target.position;
offsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
void Update () {
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - lastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget) {
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
} else {
lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
lastTargetPosition = target.position;
}
}
Don't know why I am getting this error. Everything still works but the errors are annoying. Any help would be appreciated.
Answer by smallbit · Aug 14, 2014 at 05:03 AM
The error states where the problem is! "You probably need to assign the target variable of the Camera2DFollow script in the inspector." Did you drag your target into this slot in the inspector?
yes, that was the first thing i checked. The player object is dragged under in the inspector, so that's why i was confused about getting this error.