Weird NullReferenceException Issue

I’ve been following a tutorial on setting up a mecanim character with a third person camera but having some weird issues with NullReferenceException errors that seem to occur randomly.

If I open my project and then run it, Unity will be play it fine. However at random times (usually just after I’ve changed something in the inspector) I’ll just get the NullReferenceException error. If I save my progress (with this error still occurring) close down Unity, re-open it and load my project It will run fine!

The script error I get is:

NullReferenceException: Object reference not set to an instance of an object
ThirdPersonCamera.LateUpdate () (at Assets/Scripts/ThirdPersonCamera.cs:65)

And the code relating to the error is:

using UnityEngine;
using System.Collections;

public class ThirdPersonCamera : MonoBehaviour 
{
	
	#region Variables
	
	private Transform parentRig;
	private float distanceAway = 3.5f;
	private float distanceUp = 1.0f;
	private float smooth = 10f;
	
	[SerializeField]
	private Transform followXForm;
	[SerializeField]
	private CharacterControllerLogic follow;
	
	private Vector3 lookDir;
	private Vector3 curLookDir;
	private Vector3 targetPosition;
	
	private Vector3 velocityCamSmooth = Vector3.zero;
	private float camSmoothDampTime = 0.1f;
	private Vector3 velocityLookDir = Vector3.zero;
	private float lookDirDampTime = 0.1f;
	
	private float rightStickThreshold = 0.1f;
	private const float freeRotationDegreePerSecond = 5f;
	
	#endregion
	
	// Use this for initialization
	void Start () 
	{
		parentRig = this.transform.parent;
		if (parentRig == null)
		{
			Debug.LogError("Parent camera to empty GameObject.", this);
		}
		
		follow = GameObject.FindWithTag("Player").GetComponent<CharacterControllerLogic>();
		followXForm = GameObject.FindWithTag("Player").transform;
		
		lookDir = followXForm.forward;
		curLookDir = followXForm.forward;
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	
	void LateUpdate()
	{
		float rightX = Input.GetAxis("RightStickX");
		float rightY = Input.GetAxis("RightStickY");
		float leftX = Input.GetAxis("Horizontal");
		float leftY = Input.GetAxis("Vertical");
		
		Vector3 characterOffset = followXForm.position + new Vector3(0f, distanceUp, 0f);
		Vector3 lookAt = characterOffset;
		
		if (follow.Speed > follow.LocomotionThreshold  follow.IsInLocomotion()  !follow.IsInPivot())
		{
			lookDir = Vector3.Lerp(followXForm.right * (leftX < 0 ? 1f : -1f), followXForm.forward * (leftY < 0 ? -1f : 1f), Mathf.Abs(Vector3.Dot(this.transform.forward, followXForm.forward)));
			Debug.DrawRay(this.transform.position, lookDir, Color.white);
			
			curLookDir = Vector3.Normalize(characterOffset - this.transform.position);
			curLookDir.y = 0;
			Debug.DrawRay(this.transform.position, curLookDir, Color.green);
			
			curLookDir = Vector3.SmoothDamp(curLookDir, lookDir, ref velocityLookDir, lookDirDampTime);
		}
		
		targetPosition = characterOffset + followXForm.up * distanceUp - Vector3.Normalize(curLookDir) * distanceAway;
		
		// Rotating around character
		parentRig.RotateAround(characterOffset, followXForm.up, freeRotationDegreePerSecond * (Mathf.Abs(rightX) > rightStickThreshold ? rightX : 0f));
		
		
		CompensateForWalls(characterOffset, ref targetPosition);
		smoothPosition(parentRig.position, targetPosition);
		transform.LookAt(lookAt);
	}
	
	private void smoothPosition(Vector3 fromPos, Vector3 toPos)
	{
		parentRig.position = Vector3.SmoothDamp(fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
	}
	
	private void CompensateForWalls(Vector3 fromObject, ref Vector3 toTarget)
	{
		Debug.DrawLine(fromObject, toTarget, Color.cyan);
		
		RaycastHit wallHit = new RaycastHit();
		if (Physics.Linecast(fromObject, toTarget, out wallHit))
		{
			Debug.DrawRay(wallHit.point, Vector3.left, Color.red);
			toTarget = new Vector3(wallHit.point.x, toTarget.y, wallHit.point.z);
		}
	}
}

When checking the inspector on running the game it appears that the ‘follow’ variable which is meant to be loading a component script attached to my player isn’t being set, but I have no idea why one minute it works, and the next it doesn’t and requires me to re-load Unity to work.

Any ideas?

Try putting this in the update:

if(!follow)
{
        follow = GameObject.FindWithTag("Player").GetComponent<CharacterControllerLogic>();
        followXForm = GameObject.FindWithTag("Player").transform;
        lookDir = followXForm.forward;
         curLookDir = followXForm.forward;
}

just to make sure it gets set.