respawn breaks camera

Hi,

So im struggling, i have my camera script which follows the player, upon death i respawn, but this breaks the camera with this:

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

I cannot figure it out or seem to find a suitable answer that doesnt cause more problems, so two days of googling and youtube is getting me nowhere.

heres my scripts:

PlayerRespawn

#pragma strict

var Player : GameObject;
var spawnPoint : Transform;

function OnTriggerEnter2D(other : Collider2D){
Destroy (other.gameObject);
var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
var sf = Camera.main.GetComponent(SmoothFollow2D);
sf.target = P.transform;

}

and here is the camera script:

#pragma strict

var target : Transform;
var smoothTime = 0.3;
private var thisTransform : Transform;
private var velocity : Vector2;

function Start()
{
thisTransform = transform;
}

function Update()
{
thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x,
target.position.x, velocity.x, smoothTime);
thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y,
target.position.y, velocity.y, smoothTime);
}

any help would be greatly appreciated, thanks.

So the error you are getting is because somewhere a transform is being destroyed or not assigned at all. Then something is trying to use that transform.

I also see that your are reassigning the camera’s player transform so you should NOT be getting this error but other things could be throwing this error such as your spawnPoint being null. Double check that. If spawnpoint is null then the function will crash at

 GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);

and the line

sf.target = P.transform;

will never be read, breaking your camera. and causing TWO transforms to be NULL.

I also recommend NOT destroying the player object. Instead have the player object have a death() function that will take care of reducing score, hp, and placing him at the respawn point.

Really there is no reason to destroy the player in code since all your doing is breaking links with other objects and causing more of a hassle then necessary when he dies.