UnassignedReferenceException

UnassignedReferenceException: The variable camTransform of CameraControl2 has not been assigned.
You probably need to assign the camTransform variable of the CameraControl2 script in the inspector.
UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs:20)
CameraControl2.Update () (at Assets/Scripts/CameraControl2.cs:22)

using UnityEngine;
using System.Collections;

public class CameraControl2 : MonoBehaviour {
    private bool moveCamera = false;
    public float addDistance;
    public float cameraDistance;
    public float smoothDamp = 0.5f;
    public Transform boundaryBox;
    private Vector3 startPos;
    public bool Death = false;
    public Transform camTransform;


    // Use this for initialization
    void Start () {
        startPos = transform.position;
    }
   
    // Update is called once per frame
    void Update () {
            camTransform.position = new Vector3(camTransform.position.x, camTransform.position.y, -cameraDistance);
        if(moveCamera == true) {
            cameraDistance = Mathf.Lerp (cameraDistance, -addDistance, smoothDamp);
        }

        else if (Death == true) {
            transform.position = startPos;
            Death = false;
        }
    }

    void OnTriggerEnter(Collider other) {
        if(moveCamera == false) {
            Instantiate(boundaryBox, new Vector3(2.44f,0.68f,-0.41f), Quaternion.identity);
        }
        moveCamera = true;


    }
}

The error message actually tells you exactly what your error is - your camTransform variable has not been linked to anything. Wherever you have this script attached, if you select it in the hierarchy, you’ll see in the inspector the variables associated with it. One of them is “camTransform”. You need to drag your main camera onto this. Or, if this script is on your camera, then you should set this variable in Awake or Start, like so:

camTransform = transform;
2 Likes

I’ve already assigned it in the inspector, but it still isn’t working

If you are getting the same error, but you dragged and dropped something into the camTransform slot in the inspector, then it’s possible you have multiple copies of the script in your scene, so check for that.