Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Player Input and Cameras Make a Follow Camera

Patrick Mockridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Patrick Mockridge
Full Stack JavaScript Techdegree Graduate 45,611 Points

The Camera isn't Following the Frog!

using UnityEngine; using System.Collections;

public class FollowCamera : MonoBehaviour {

[SerializeField]
private Transform player;
[SerializeField]
private Vector3 offset;
private float cameraFollowSpeed = 5f;


// Update is called once per frame
void Update () {
    Vector3 newPosition = player.position + offset;

    transform.position = Vector3.Lerp (transform.position, newPosition, cameraFollowSpeed * Time.deltaTime);
}

}

I'm getting the following error in Unity:

'UnassignedReferenceException: The variable player of FollowCamera has not been assigned. You probably need to assign the player variable of the FollowCamera script in the inspector.'

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code differs a bit from mine on this line:

transform.position = Vector3.Lerp (transform.position, newPosition, cameraFollowSpeed * Time.deltaTime);

Mine adds Time.deltaTime

transform.position = Vector3.Lerp (transform.position, newPosition, cameraFollowSpeed + Time.deltaTime);

However, I don't believe that to be the cause of this error. I feel like the FollowCamera script that's connected to the Main Camera doesn't have the Player assigned to it. Try this. Click on the "Main Camera" in the hierarchy and scroll down to where you see the script connected to it. You should see a box that shows the FollowCamera script, a box that says Player, and a box that says Offset. Make sure your Player is in the text box to the right of the Player label.

Hope this helps! :sparkles: