- Home /
Why i'm getting UnassignedReferenceException when running the game ?
The script is attached to Main Camera. In the inspector i dragged to the script to target a Cylinder from the Hierarchy. The camera does rotate and when i'm using a break point on the line number 19:
_direction = (target.position - transform.position).normalized;
target is not null. And it does the rotation. So why i'm getting this exception ?
This is the exception message:
UnassignedReferenceException: The variable target of LookAtCamera has not been assigned. You probably need to assign the target variable of the LookAtCamera script in the inspector. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:26) LookAtCamera.Update () (at Assets/MyScripts/LookAtCamera.cs:19)
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamera : MonoBehaviour {
//values that will be set in the Inspector
public Transform target;
public float RotationSpeed;
//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;
// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
_direction = (target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}
Answer by bobisgod234 · May 19, 2017 at 12:14 AM
Are you sure you don't have multiple instances of the script in the scene? try putting
t:LookAtCamera
Into the Hierarchy search field. This will highlight all objects with a LookAtCamera component attached.
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers