- Home /
Object not set to a instance of a object
I have no clue what I am doing wrong.
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
[SerializeField]
private Transform target = null;[SerializeField]
private float distance = 3.0f;[SerializeField]
private float height = 1.0f;[SerializeField]
private float damping = 5.0f;[SerializeField]
private bool smoothRotation = true;[SerializeField]
private float rotationDamping = 10.0f;
[SerializeField]
private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
[SerializeField]
private float bumperDistanceCheck = 5f; // length of bumper ray
[SerializeField]
private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
[SerializeField]
private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
private Transform Player; /// <Summary>
/// If the target moves, the camera should child the target to allow for smoother movement. DR
/// </Summary>
private void Awake()
{
GetComponent<Camera>().transform.parent = Player;
}
private void FixedUpdate()
{
Vector3 wantedPosition = Player.TransformPoint(0, height, -distance);
// check to see if there is anything behind the target
RaycastHit hit;
Vector3 back = Player.transform.TransformDirection(-1 * Vector3.forward);
// cast the bumper ray out from rear and check to see if there is anything behind
if (Physics.Raycast(Player.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
&& hit.transform != target) // ignore ray-casts that hit the user. DR
{
// clamp wanted position to hit position
wantedPosition.x = hit.point.x;
wantedPosition.z = hit.point.z;
wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
Vector3 lookPosition = Player.TransformPoint(targetLookAtOffset);
if (smoothRotation)
{
Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, Player.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else
transform.rotation = Quaternion.LookRotation(lookPosition - Player.position, target.up);
}
}
Answer by Commoble · Apr 03, 2017 at 05:15 PM
A null reference exception happens when you try to call someObject.someMember
when someObject is null. Your error message will tell you which line this happened in. Look at that line, figure out which null variable you're trying to access, and decide whether A) it really should never be null there and there's a problem that needs to be fixed, or B) you can do
if (someObject == null)
{
// do something
}
else
{
// do something else
}
Sorry I forgot to elaborate.
Vector3 wantedPosition = Player.TransformPoint(0, height, -distance);
is the NullRefrenceException and target is assigned as the Player. I tried to change target = null; to just target but the results are the same
Your Player variable might be null. $$anonymous$$ake sure it's being assigned.
Your answer
Follow this Question
Related Questions
Problem with the camera, 2D. 0 Answers
Does anyone know why this simple player camera lock script is not working? 0 Answers
How to adjust camera position/rotation when player looks up or down? (Third Person Camera) 1 Answer
BoxCollider2d.bounds.Intersects Not Working 2 Answers
How to rotate a cube on its edge and have the cube able to climb up other surfaces 0 Answers