UnassignedReferenceException

UnassignedReferenceException: The variable thingToFollow of FollowCamera has not been assigned.
You probably need to assign the thingToFollow variable of the FollowCamera script in the inspector.
FollowCamera.LateUpdate () (at Assets/FollowCamera.cs:12)

This is the error I keep receiving even though I have assigned the car as the thing to follow. The game works as intended so far with the camera following the car perfectly as I drive around, but I keep getting this error.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class FollowCamera : MonoBehaviour

{

[SerializeField] GameObject thingToFollow;

// this things position (Camera) should be the same as the car's position

void LateUpdate()

{

   transform.position = thingToFollow.transform.position + new Vector3 (0,0,-10);

}

}

Usually in situations like this, the problem is that there is another FollowCamera component on a game object you didn’t expect somewhere in the hierarchy.
There are ways to find it in Unity, or if you use Visual Studio it can show you which game objects references it

or you could add this Start method

private void Start()
{
    Debug.Log($"FollowCamera attached to {gameObject.name}");
}

It will write to console and if there are more than one, you will see where the one is that shouldn’t be there.


As a side note; You aren’t doing anything with the ‘game object’ of thingToFollow. You can change the type of the variable to Transform for a miniscule improvement in performance

[SerializeField] Transform thingToFollow;

void LateUpdate()
{
    transform.position = thingToFollow.position + new Vector3 (0,0,-10);
}
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms