i am making a 2D platformer and ive added a cameraa follow player script. Exept when you die and respawn the camera follow script stopps working and i get this error message could somebody please help me? thank you. im quite new to unity so if you could simplify that would be much appreciated.
this isthe error message 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.Update () (at Assets/Scenes/CameraController.cs:18)
The answer is always the same… ALWAYS!
How to fix a NullReferenceException error
Three steps to success:
- Identify what is null ← any other action taken before this step is WASTED TIME
- Identify why it is null
- Fix that
NullReference is the single most common error while programming. Fixing it is always the same.
Some notes on how to fix a NullReferenceException error in Unity3D:
ALSO…
Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.
There’s even a dedicated Camera / Cinemachine area: see left panel.
If you insist on making your own camera controller, do not fiddle with camera rotation.
The simplest way to do it is to think in terms of two Vector3 points in space:
- where the camera is LOCATED
- what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;
void LateUpdate()
{
cam.transform.position = WhereMyCameraIsLocated;
cam.transform.LookAt( WhatMyCameraIsLookingAt);
}
Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.
Hey,
this sounds like your camera script is still accessing the old player gameobject, which died i.e. got destroyed.
Solution: After you spawn a new player object, you need to update the reference inside the camera script to point to the newly instantiated player gameobject.
Im pretty sure that the problem is that i set it up with prefabs so that when you die it respawns a clone and that when the camera trys to access the player its accessing the original one instead of the new clone? Sorry for asking again just a bit confused.
heres my camera Follow script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public GameObject Player;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
transform.position = new Vector3(Player.transform.position.x, transform.position.y, transform.position.z);
}
}
/
Please use tags that directly relate to the problem rather than things you might simply be using. 2D-Physics isn’t related to the Camera/Scripts which is what your post is about.
I’ll go ahead and remove that tag and simply add the “Scripting” tag.
Thanks.
Sorry thank you im new to unity discussions
Its ok i fixed it thankyou