How do I get my 2D game camera to view the respawned sprite?

I’m brand new to Unity, and I’m having a bit of trouble. I’m creating a 2D platformer very much like Mario. When the player falls down a hole, another is created at the spawn point, but the camera doesn’t look at it.

I have a script for my camera to follow my player, but when my player dies, I not only get the error:

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/Scripts/CameraFollow.js:5)

But my camera also doesn’t relocate to the spawn point and look at my instantiated character. I’m not sure what I have to do to code this properly.

The code on my camera is very simple [title: CameraFollow]:

    var player : GameObject;
    function Update () {
    this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);

And the code on my Player Respawn script is relatively simple [title: PlayerRespawn]:

#pragma strict

var Player : GameObject;
var spawnPoint : Transform;

function OnTriggerEnter(other : Collider) {
		var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
		var cf = Camera.main.GetComponent(CameraFollow);
		var instance : GameObject;

Can anybody explain to me, in a way that allows a n00b to comprehend it, how to get unity to stop telling me that my gameobject is destroyed and needs to either be null/not destroyed (basically my error code), and especially how to relocate the camera to the new player which IS being created–it’s just not being looked at by my Main Camera.

Thanks for the help, and I’m sorry if I didn’t explain things thoroughly enough.

Just add:

cf.player = gameObject;


var cf = Camera.main.GetComponent(CameraFollow);

Basically what happens is, You destroy the gameObject (player) when you fall into a hole.

Now, the camera is trying to follow a destroyed gameObject.

That is why you are getting the error.

You can fix that in two ways, either by telling the camera to follow the new player you are creating at spwan (like my code above) or, by not destroying the player and only moving it back to spwan point.