3

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);
    }
}  
1
  • then it might not have been the correct field ;) or you maybe have multiple components of that type and only assigned one of them. Click on the error during the playmode and it should highlight the object in the hierarchy for which the exception was thrown Commented Aug 29, 2019 at 5:03

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Haha! Assigning it to another object was the mistake. No idea how but somehow the script ended up on the ground as well. Thanks a million good sir!
@RustyBucketBay yep that was basically my comment on your question ;)
@helplesscodenub flag his answer as solution if it worked for you
0

There are multiple things you can try to do:

  1. Make sure you dragged the correct object in the field (I doubt that your character object is called CameraFollow)
  2. Make sure that you dragged in an object from the Hierarchy and not the Assets window (ethats means that you need to drag in objects that are currently in the scene and can be seen on the hierarchy)
  3. If you try everything from above and it doesen't work try assigning the object in the start function of the script. You can use GameObject.Find

Hope this helped to clarify a few things for you. Now if you really want to create a top level camera system you can also check this video out. Its an example of how to make a third person camera with the Cinemachine component (comes with Unity package manager for free)

I wish you luck with coding in Unity and welcome to the community :)

2 Comments

I was watching a tutorial which said to name the object "camera follow" interestingly enough it was made by the same person! youtu.be/LbDQHv9z-F0?t=987. The object was created in the Hieratchy, and thanks for the welcome!
The only thing that could possibly go wrong now is maybe because your player object is a prefab and you added a new child that is not applied. Try going to the root object for the character, right clicking and selecting the option to unpack prefab completely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.