- Home /
NullReferenceException: Object reference not set to an instance of an object CameraFollow.Update ()
So i have setup a CameraFollow script that enables me to follow my player . But when i destroy the player on gameover , i get this error on the CameraFollow script "NullReferenceException: Object reference not set to an instance of an object CameraFollow.Update ()" i know its because the camera is trying to access the players transform .But how do i fix this error? How do i make the camera stop following player's transform when the player object is destroyed? Here's the CameraFollow Script Code :
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour {
public float MinY;
public float MaxY;
private Transform Target;
// Use this for initialization
void Start ()
{
Target = GameObject.Find("Player").transform;
}
// Update is called once per frame
void Update ()
{
if (!GameObject.Find("Player").GetComponent<ScoreManager>().PlayerIsDead)
{
transform.position = new Vector3(
Mathf.Clamp(Target.position.x, Target.position.x, Target.position.x),
Mathf.Clamp(Target.position.y, MinY, MaxY),
Mathf.Clamp(transform.position.z, transform.position.z, transform.position.z));
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612092847im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I use mathf.clamp to make boundaries for a camera 0 Answers
lerping transform.position on a camera 0 Answers
How do I update values and the transform of an object on the server's and client's side? 0 Answers
Spawn objects at bottom of screen / camera 2 Answers
Multiple Targets Camera 1 Answer