0
\$\begingroup\$

My current task is to make the camera follow the moving cube in a stable way where the camera won't get locked in the center of the object so it won't get dizzy once the cube starts to collide or fall down.

I have done the parenting right, I have named the cube as the player and then selected it as the Transform reference in the new component that I have created for the camera to follow.

But it just says:

UnassignedReferenceException : The variable player of FollowPlayer has not being assigned.

and I am unable to get the right results. What should I do ?

Here is the C# script that I wrote:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {
    public Transform player;
    public Vector3 offset;

    void Update() {
        transform.position = player.position + offset;
    }
}

My scene setup looks like this:

Screenshot of Unity editor showing "Player" variable correctly assigned

So I have un-parented the main camera from the player and it works perfectly fine but I still get the error, Have any ideas why?

\$\endgroup\$
0
0
\$\begingroup\$

Check to see if you have a stray second instance of your FollowPlayer script somewhere in your scene, with its player variable unset.

You can have every instance print its name to the debug console in Start() to help track it down.

If the camera's reference to the Player transform was deleted or cleared, you'd get a MissingReferenceException or NullReferenceException instead, so this is almost certainly a scene & inspector setup issue.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.