Getting NullReferenceException: but cannot figure out why

Hi there,

I have the following two scripts which I have seen working in a video but when I have recreated the scripts and try to run them I get the following

NullReferenceException: Object reference not set to an instance of an object

TP_Motor.ProcessMotion () (at Assets/Scripts/TP_Motor.cs:41)

TP_Motor.UpdateMotor () (at Assets/Scripts/TP_Motor.cs:21)

TP_Controller.Update () (at Assets/Scripts/TP_Controller.cs:37)

Here are the compete two scripts, I will mark the relevant lines its saying.

using UnityEngine;
using System.Collections;

public class TP_Controller : Photon.MonoBehaviour 
{
	public static CharacterController CharacterController;
	public static TP_Controller Instance;

	void Awake() 
	{
		CharacterController = GetComponent ("CharacterContoller") as CharacterController;
		Instance = this;
	}
	
	void Update() 
	{
		if (Camera.main == null)
			return;

		GetLocomotionInput ();

	
		TP_Motor.Instance.UpdateMotor (); // Line 37 Error in TP_Controller
	}

	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);
		}
	}
}

and

using UnityEngine;
using System.Collections;

public class TP_Motor : Photon.MonoBehaviour 
{
	public static TP_Motor Instance;

	public float MoveSpeed = 10f;

	public Vector3 MoveVector { get; set; }

	void Awake() 
	{
		Instance = this;
	}
	
	public void UpdateMotor() 
	{
		SnapAlignCharacterWithCamera ();

		ProcessMotion (); //Line 21 Error in TP_Motor
	}

	void ProcessMotion()
	{

		// Transform MoveVector to World Space
		MoveVector = transform.TransformDirection (MoveVector);

		// Normalize Move Vector if Magnitude > 1
		if (MoveVector.magnitude > 1)
			MoveVector = Vector3.Normalize (MoveVector);

		// Multiply MoveVector by MoveSpeed
		MoveVector *= MoveSpeed;

		// Multiply MoveVector by DeltaTime
		MoveVector *= Time.deltaTime;

		// Move the Character in World Space
	
		TP_Controller.CharacterController.Move (MoveVector); //Line 41 error in TP_Motor

	}

	void SnapAlignCharacterWithCamera()
	{
		if (MoveVector.x != 0 || MoveVector.z != 0) 
		{
			transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.main.transform.eulerAngles.y, transform.eulerAngles.z);
		}
	}
}

I have looked over it and I just cannot figure out why its erroring.

Thanks

Doomie

Looks like it is erroring because CharacterController is null - do you have the TP_Controller script attached to something with a CharacterController?

A debug log in the Start to ensure it is not null would be the first plan.

Also, set CharacterController (should really be characterController for a variable BTW) with GetComponent() - no need for the cast and it’s faster