Home > front end >  MissingReferenceException when gameObject has been destroyed
MissingReferenceException when gameObject has been destroyed

Time:12-30

I'm trying to learn how to make camera follow player in Unity. everything went fine. the scripts works, no problem with the game, it just when my Character(Player) dead(destroyed), the console keep updating this error message

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at <f53c831f77784ef08ef348217b5117fa>:0) CameraClamp.Update () (at Assets/Scripts/CameraClamp.cs:12)

and this

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. CameraFollow.FixedUpdate () (at Assets/Scripts/CameraFollow.cs:25)

and here's the scripts

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

public class CameraFollow : MonoBehaviour
{

    public GameObject FollowObject;
    public Vector2 followOffset;
    public float speed = 3f;

    private Rigidbody2D rb;
    private Vector2 threshold;
    // Start is called before the first frame update
    void Start()
    {
     threshold = calculateThreshold();
     rb = FollowObject.GetComponent<Rigidbody2D>();
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector2 follow = FollowObject.transform.position;
        float xDifference = Vector2.Distance(Vector2.right * transform.position.x, Vector2.right * follow.x);
        float yDifference = Vector2.Distance(Vector2.up * transform.position.y, Vector2.up * follow.y);

        Vector3 newPosition = transform.position;
        if(Mathf.Abs(xDifference) >= threshold.x)
        {
            newPosition.x = follow.x;
        }
        if(Mathf.Abs(yDifference) >= threshold.y)
        {
            newPosition.y = follow.y;
        }

        float moveSpeed =  rb.velocity.magnitude > speed ? rb.velocity.magnitude : speed;
        transform.position = Vector3.MoveTowards(transform.position, newPosition, moveSpeed * Time.deltaTime);
    }

    private Vector3 calculateThreshold(){
        Rect aspect =  Camera.main.pixelRect;
        Vector2 t = new Vector2 (Camera.main.orthographicSize * aspect.width / aspect.height, Camera.main.orthographicSize);
        t.x -= followOffset.x;
        t.y -= followOffset.y;
        return t;
    }
    private void OnDrawGizmos() {
        Gizmos.color = Color.blue;
        Vector2 border = calculateThreshold();
        Gizmos.DrawWireCube(transform.position, new Vector3(border.x * 2, border.y * 2, 1));
        
    }
}

How to get rid of the error message?

CodePudding user response:

There's two reasonable solutions

  1. When you delete the player, you should delete this script too. This could be achieved by deleting the first parent of both FollowObject, CameraFollow, and Camera lamp. If they don't have a common parent, consider refactoring it so they do

  2. Check for null before accessing the transform

    void FixedUpdate() { if(FollowObject.transform == null) { Delete(this); } }

  • Related