Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity "Unassigned Reference Exception"

I'm super new to coding and to this forum so forgive me if I break any taboo's here. I'm simply working on a 3rd person camera, just kind of messing around but I keep getting

UnassignedReferenceException: The variable CameraFollowObj of CameraFollow has not been assigned.
You probably need to assign the CameraFollowObj variable of the CameraFollow script in the inspector.
CameraFollow.CameraUpdater () (at Assets/Scripts/CameraFollow.cs:68) CameraFollow.LateUpdate () (at Assets/Scripts/CameraFollow.cs:62)"

I've created an object for my camera to follow and placed it on the model. Then moved the object to what I believe to be the correct field but the issue still persists.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public float CameraMoveSpeed = 120.0f;
    public GameObject CameraFollowObj;

    Vector3 FollowPOS;

    public float clampAngle = 80.0f;
    public float InputSensitivity = 150.0f;
    public GameObject CameraObj;
    public GameObject PlayerObj;
    public float camDistanecXToPlayer;
    public float camDistanecYToPlayer;
    public float camDistanecZToPlayer;
    public float mouseX;
    public float mouseY;
    public float finalInputX;
    public float finalInputZ;
    public float smoothX;
    public float smoothY;

    private float rotY = 0.0f;
    private float rotX = 0.0f;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 rot = transform.localRotation.eulerAngles;
        rotY = rot.y;
        rotX = rot.x;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {
        float InputX = Input.GetAxis("RightStickHorizontal");
        float InputZ = Input.GetAxis("RightStickVertical");
        mouseX = Input.GetAxis("Mouse X");
        mouseY = Input.GetAxis("Mouse Y");
        finalInputX = InputX + mouseX;
        finalInputZ = InputZ + mouseY;

        rotY += finalInputX * InputSensitivity * Time.deltaTime;
        rotX += finalInputZ * InputSensitivity * Time.deltaTime;

        rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

        Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
        transform.rotation = localRotation;
    }

    void LateUpdate() 
    {
        CameraUpdater();
    }

    void CameraUpdater() 
    {
        Transform target = CameraFollowObj.transform;

        float step = CameraMoveSpeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards (transform.position, target.position, step);
    }
}  
like image 964
helplesscodenub Avatar asked Feb 02 '26 10:02

helplesscodenub


1 Answers

Make sure you haven't added the script to another gameobject somewhere else in the project that might cause this error. You can search for the script in the scene search bar and all the gameObjects with the script attached will appear. Also in runtime if you right click on the script and in the contextual menu you select option kind of "find all the references in the scene" or something similar, you get all the instances of the script in your scene.

I think you should have drargged the script into another gameObject by mistake where the cameraToFollow gameObject is empty so you get the unnasigned error.

Hope this helps.

like image 179
rustyBucketBay Avatar answered Feb 05 '26 00:02

rustyBucketBay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!