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 Cameras in Unity Camera Movement Adding Camera Bob

Ben Reynolds
Ben Reynolds
35,170 Points

The variable target of OrbitCamera has not been assigned

The "Camera 1" empty game object does not show the script component belonging to the cinematic camera in the inspector. If I try to manually add the Orbit Camera script component to Camera 1 (click Add Component and select the script) and set the target to the board object I get the error:

UnassignedReferenceException: The variable target of OrbitCamera has not been assigned. You probably need to assign the target variable of the OrbitCamera script in the inspector. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:27) OrbitCamera.Update () (at Assets/Scripts/OrbitCamera.cs:37)

Unity 2017.1.0f3 personal

Here's my script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OrbitCamera : MonoBehaviour {

    [SerializeField]
    private Transform target;

    [SerializeField]
    private float rotationSpeed = 10f;

    [SerializeField]
    private float bobPeriod = 2f;
    [SerializeField]
    private float bobAmplitude = 2f;

    private float startY;
    private float bobDistance;

    // Use this for initialization
    void Start () {

        // Store the start position of the camera
        startY = transform.localPosition.y;
    }

    // Update is called once per frame
    void Update () {

        // Update the vertical camera bob
        bobDistance = Mathf.Sin(Time.timeSinceLevelLoad / bobPeriod) * bobAmplitude;

        transform.position = new Vector3 (transform.localPosition.x, startY + bobDistance, transform.localPosition.z);

        // Update the camera position
        transform.RotateAround (target.position, Vector3.up, rotationSpeed * Time.deltaTime);

        // Update the camera look
        transform.LookAt (target);

    }
}

2 Answers

Ben Reynolds
Ben Reynolds
35,170 Points

I was able to get it working by disabling the script on the Cinematic Camera object. The confusion came from the previous video where the script was created and added on the Cinematic Camera object, and not on its parent (Camera 1).

Then in this video, the script is shown as a component on the Camera 1 empty object instead of the camera itself. So I added the script component to Camera 1, but I had left it enabled on the child camera object as well, and things went haywire with the script running on both objects at once (each object was expecting its own value for "target").

So it looks like a step was left out of the tutorial where the script is moved from the camera object to the empty object, or my version of Unity (2017.1.0f3) may behave differently than whatever version was used in the video. This series is my first experience with Unity, so there could be some other simple step I missed along the way. Hopefully this helps anybody else who runs into this issue.

Alan Mattan贸
PLUS
Alan Mattan贸
Courses Plus Student 12,188 Points

You can麓t assign a script with errors to a Game Object. First, you need to fix the error in the script and then assign it as a component into the game object. You can use drag and drop, by selecting the script, drag it into the game object or drop it into the Inspector as a new component. In the console, the error will pop up. It tells you the script and line where is the error. You can double-click the error message in the console and Visual Studio will open showing the script and the cursor next to the line where is the error. The error can be near the cursor, including the before code line. When you finish correcting the error and there is no error in console, you can assign that script to your game object.

When you are creating a new script is a good practice to include the script into the game object as a component. Later, when you create a new variable as for example "target" game object, you need to assign the reference as soon as possible to the inspector. In this way, you do not get UnassignedReferenceException and avoid problems.