C# camera script error

Hello, I’ve been following a tutorial all the steps and I think I missed something cus I’m getting a null reference error and my camera aint following my Character. It’s a 3rd person character Im trying to create
using UnityEngine;
using System.Collections;

public class TP_Controller : MonoBehaviour 
{
	public static CharacterController Charactercontroller;
	public static TP_Controller Instance;

	void Awake () 
	{
		Charactercontroller = GetComponent ("CharacterController") as CharacterController;
		Instance = this;
		TP_Camera.UseExistingOrCreateNewMainCamera ();
	}
	void Update () 
	{
		if (Camera.mainCamera == null) 
		{
			return;
		}
		GetLocoMotionInput ();

		TP_Motor.Instance.UpdateMotor ();
	}
	void GetLocoMotionInput()
	{
		var deadzone = 0.1f;

		TP_Motor.Instance.MoveVector = Vector3.zero;

		if(Input.GetAxis("Vertical") > deadzone || Input.GetAxis("Vertical") < -deadzone)
			TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));


		if(Input.GetAxis("Horizontal") > deadzone || Input.GetAxis("Horizontal") < -deadzone)
			TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
	}
}

I tried going back to the tutorials and I can’t find it, the problem I got now is that the camera aint following the character and if anyone could help me resolve that problem. It said I got an null reference, instance not set to an object, something like that at this line

		TP_Motor.Instance.MoveVector = Vector3.zero;

I would be greatfull if anyone could help me.

EDIT 1:

Here’s the TP_Camera script:

using UnityEngine;
using System.Collections;
 
public class TP_Camera : MonoBehaviour 
{
    public static TP_Camera Instance;
    public Transform TargetLookAt;
    public float Distance = 5f;
    public float DistanceMin = 3f;
    public float DistanceMax = 10f;
    public float X_mouseSensitivity = 5f;
    public float Y_mouseSensitivity = 5f;
    public float mouseWheelSensitivity = 5f;
    public float Y_MinLimit = -40f;
    public float Y_MaxLimit = 80f;
    public float distanceSmooth = 0.05f;
    private float velDistance = 0f;
    public float X_Smooth = 0.05f;
    public float Y_Smooth = 0.1f;
 
    private float MouseX = 0f;
    private float velX = 0f;
    private float velY = 0f;
    private float velZ = 0f;
    private float MouseY = 0f;
    private float StartDistance = 0f;
    private Vector3 desiredPosistion = Vector3.zero;
    private float DesiredDistance = 0f;
    private Vector3 Posistion = Vector3.zero;
 
 
 
    void Awake()
    {
       Instance = this;
    }
 
 
    void Start() 
    {
       Distance = Mathf.Clamp (Distance, DistanceMin, DistanceMax);
       StartDistance = Distance;
       Reset ();
    }
 
    void LateUpdate() 
    {
       if (TargetLookAt == null) 
         return;
 
       HandlePlayerInput ();
       CalculateDesiredPosistion ();
       UpdatePosistion ();
 
 
    }
    void HandlePlayerInput()
    {
       var deadZone = 0.01f;
       if (Input.GetMouseButton (1)) 
       {
         MouseX += Input.GetAxis("Mouse X") * X_mouseSensitivity;
         MouseY -= Input.GetAxis("Mouse Y") * Y_mouseSensitivity;
       }
 
       MouseY = Helper.ClampAngle (MouseY, Y_MinLimit, Y_MaxLimit);
 
       if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone || Input.GetAxis ("Mouse ScrollWheel") > deadZone) 
       {
         DesiredDistance = Mathf.Clamp(Distance - Input.GetAxis ("Mouse ScrollWheel") * mouseWheelSensitivity, DistanceMin, DistanceMax);
       }
 
 
 
    }
    public void Reset()
    {
       MouseX = 0;
       MouseY = 10;
       Distance = StartDistance;
       DesiredDistance = Distance;
    }
    void CalculateDesiredPosistion()
    {
       Distance = Mathf.SmoothDamp (Distance, DesiredDistance, ref velDistance, distanceSmooth);
 
       desiredPosistion = CalculatePosistion (MouseY, MouseX, Distance);
    }
    Vector3 CalculatePosistion(float rotationX, float rotationY, float distance )
    {
       Vector3 direction = new Vector3 (0, 0, -distance);
 
       Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
       return TargetLookAt.position + rotation * direction;
 
 
    }
    void UpdatePosistion()
    {
       var posX = Mathf.SmoothDamp (Posistion.x, desiredPosistion.x, ref velX, X_Smooth);
       var posY = Mathf.SmoothDamp (Posistion.y, desiredPosistion.y, ref velY, Y_Smooth);
       var posZ = Mathf.SmoothDamp (Posistion.z, desiredPosistion.z, ref velZ, X_Smooth);
       Posistion = new Vector3 (posX, posY, posZ);
 
       transform.position = Posistion;
 
       transform.LookAt (TargetLookAt);
    }
 
 
    public static void UseExistingOrCreateNewMainCamera()
    {
       GameObject tempCamera;
       GameObject targetLookAt;
       TP_Camera myCamera;
 
       if (Camera.mainCamera != null) 
       {
         tempCamera = Camera.mainCamera.gameObject;
       } 
       else 
       {
         tempCamera = new GameObject("Main Camera");
         tempCamera.AddComponent("Camera");
         tempCamera.tag = "MainCamera";
       }
       tempCamera.AddComponent ("TP_Camera");
       myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
 
       targetLookAt = GameObject.Find ("TargetLookAt") as GameObject;
 
 
       if (targetLookAt == null) 
       {
         targetLookAt = new GameObject("targetLookAt");
         targetLookAt.transform.position = Vector3.zero;
       }
       myCamera.TargetLookAt = targetLookAt.transform;
 
    }
}

Hey bud, do you just want your camera to follow you third person controller?

Below is a script that will follow your player , you must attach your player game object in the inspector to it.

For best result, place an empty game object in your scene, add this script to that and child your camera to the empty game object at (0,0,0.
Then in the inspector, you can fiddle with the height and rotation inc height and rotation dampening for your convenience.
This will then undoubtedly follow your player.

With respect to what you were doing above, Icouldnt see that you were calling your camera at any stage or indeed changing its position based on your TP_Controller. however, I am unfamiliar with how you are doing it generally…?
Are you using a Plugin package for your 3rd person controller?
Is it a 3DBuzz tutorial or something?

RE, these type of calls

TP_Camera.UseExistingOrCreateNewMainCamera ();

Anyway, the below script will indeed solve your camera following problem. Perhaps you could addend your TP_Control system to reflect.

using UnityEngine;

public class FollowCamera : MonoBehaviour 
{
	/*
	 * Class members
	 */
	public Transform _target;
	public float _distance = 10.0f;
	public float _height = 1.0f;
	public float _damping = 5.0f;
	public float _rotationDamping = 10.0f;
	
	
	/*
	 *  Update class function
	 */
	private void FixedUpdate()
	{
		// Calculate and set camera position
		Vector3 desiredPosition = this._target.TransformPoint(0, this._height, -this._distance);
		this.transform.position = Vector3.Lerp(this.transform.position, desiredPosition, Time.deltaTime * this._damping);
		
		// Calculate and set camera rotation
		Quaternion desiredRotation = Quaternion.LookRotation(this._target.position - this.transform.position, this._target.up);
		this.transform.rotation = Quaternion.Slerp(this.transform.rotation, desiredRotation, Time.deltaTime * this._rotationDamping);
	}
}

Take care bud, hope it helps some, if not…?!
It may help us further to see your TP_Camera class to divulge whats happening when that call is fired (I.E Whether TP_camera is supposed to be doing the positoinal updates based from TP_Controller or/and TP_Motor)
Gruffy

EDIT: If it is a 3DBuzz tutorial, did you set the third person controller object in the script of your Camera class?