Object reference not set to an instance of an object when targeting a Clone

Hey guys I really have my hands tied here I searched far and wide and can’t possibly fathom what’s the problem with my scripts. The problem is as easy as this:

I have a car racing game, in that game there is a function inside one script who instantiates an GameObject, I then need to set my car camera to target that clone so that it follows the car and allows the player to effectively control it. Problem is the way I have my code the debug tells me that the “Object reference not set to an instance of an object”. Here comes the code.

This function creates a local player (I’m trying to implement multiplayer on the game):

// Create a local player.
    private void CreateLocalPlayer()
		var player = (GameObject)Instantiate(GameManager.Manager.PlayerPrefab, UnityEngine.Vector3.zero, transform.rotation);
		if (player == null)
		// create all the components required
		//var controller = player.GetComponent<CarController>();
		// set the center and radius of the character
		player.transform.position = new Vector3(931, 101, 819);
		player.transform.rotation = Quaternion.Euler(0, 130, 0);
		// Place the main camera on the player
		GameObject.Find("MainCamera").GetComponent<CarCamera>().target = player.transform;

		this.localPlayer = player.AddComponent<Player>();
		this.localPlayer.CharacterName = GameManager.Manager.PlayerName;
		this.localPlayer.Match = this.match;

And here is the script for the CarCamera that should have the above clone as a target:

public class CarCamera : MonoBehaviour
	public Transform target = null;
	public float height = 1f;
	public float positionDamping = 3f;
	public float velocityDamping = 3f;
	public float distance = 4f;
	public LayerMask ignoreLayers = -1;

	private RaycastHit hit = new RaycastHit();

	private Vector3 prevVelocity = Vector3.zero;
	private LayerMask raycastLayers = -1;
	private Vector3 currentVelocity = Vector3.zero;
	void Start()
		raycastLayers = ~ignoreLayers;

	void FixedUpdate()
		if(target != null) {
			currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
			currentVelocity.y = 0;
			prevVelocity = currentVelocity;
	void LateUpdate()
		if(target != null) {
			float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
			camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
			float currentDistance = Mathf.Lerp(7.5f, 6.5f, speedFactor);
			currentVelocity = currentVelocity.normalized;
			Vector3 newTargetPosition = target.position + Vector3.up * height;
			Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
			newPosition.y = newTargetPosition.y;
			Vector3 targetDirection = newPosition - newTargetPosition;
			if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
				newPosition = hit.point;
			transform.position = newPosition;

	public void setTarget(Transform newTarget) {
		target = newTarget;

The debug gives the object reference error on this line:

// Place the main camera on the player
GameObject.Find("MainCamera").GetComponent<CarCamera>().target = player.transform;

If anyone can help me solve this mystery I will be greatly appreciate.

Are you absolutely sure the game object is named “MainCamera” without the space? The default name is “Main Camera” with a space between the ‘n’ and the ‘C’. Note if you’ve left the tag alone, you can use Camera.main.