Unity 6 Roll a Ball Game error

Hi Guys, New to unity.
I am getting the error message while the Enemy destroy the player.
[10:14:39] MissingReferenceException:The object of type “UnityEngine.BameObject” has been destroyed but you are still trying to access it. your script should either if it is null or you should not destroy the object? error highlights transform.position = player.transform.position + offset;

any help please bellows are my scripts:
using UnityEngine;

public class CameraController : MonoBehaviour
{
// Reference tot he payer GameObject.
public GameObject player;

// The distance between the camera and the player.
private Vector3 offset;



// Start is called before the first frame Update.
void Start()
{
 // Calcuate the initial offset between the camera's and position and the player's position. 
    offset = transform.position - player.transform.position;
}

// LateUpdate is called once per frame after all Update functions have been completed.
void LateUpdate()
{
    // Maintain the same offset between the camera and player throughout the game.
       transform.position = player.transform.position + offset;

   
}

}

Hey there, i’m quite new to unity as well and i had the same error. Just writing this fixed it for me:

void LateUpdate()
    {
        if (player != null)
        {
            transform.position = player.transform.position + offset;
        }
    }

So the camera only follows the player as long as he exists.

Edit: I just saw that i’m kind of late with this reply, hope it still helps somebody.

1 Like