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

1 Answer 1

1
\$\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\$
2
  • \$\begingroup\$ 3 years later someone finally upvotes your answer 😉 \$\endgroup\$
    – Brad
    Oct 11, 2021 at 3:03
  • \$\begingroup\$ ...and 9 community bumps... :P \$\endgroup\$
    – Vaillancourt
    Oct 11, 2021 at 3:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .