Switch camera to cloned object.

Hello again.

I have a problem with my camera, it follows a player like a 3rd person camera, it is fine but when my player dies an instantiated ragdoll is cloned and player object is destroyed. So I want my camera follows that ragdoll. I got this so far:

#pragma strict
var target : Transform;
var height : float;
var distance : float;
var shoulderView : float;

private var targetPosition : Vector3;
function Update(){
		var sf : _cameraScript = Camera.main.GetComponent(_cameraScript);
		sf.target = transform.Find("Ragdoll(Clone)");

function LateUpdate () {

	targetPosition = target.position + target.right * shoulderView + target.up * height - target.forward * distance;
	transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 30.0);


I’ve tried things like replacing target with another variable finding the clone with tag etc…
And I have the same error: NullReferenceException at targetPosition variable in LateUpdate.

Thanks in advance.

Just FYI. you definitely shouldn’t be doing this sort of thing in Update. Don’t forget, update happens 100s of times every second. if you think about it, it doesn’t make sense to check “is player dead” 100s of times a second. you need to swap the camera AT THE TIME the gameobject is destroyed, when it happens.

further, really you just shouldn’t use LateUpdate, it’s very unnecessary and almost certainly mistaken here. hope it helps a bit!