NullReferenceException: Object reference not set to an instance of an object
TP_Motor.ProcessMotion () (at Assets/Script/TP_Motor.cs:54)
TP_Motor.UpdateMotor () (at Assets/Script/TP_Motor.cs:28)
TP_Controller.Update () (at Assets/Script/TP_Controller.cs:27)
takes me to the line: TP_Controller.CharacterCountroller.Move(MoveVector); //in TP_Motor
TP_Motor code:
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour
{
public static TP_Motor Instance;
public float MovSpeed = 10f;
public float Gravity = 21f;
public Vector3 MoveVector { get; set; }
// Use this for initialization
void Awake()
{
Instance =this;
}
// Update is called once per frame
public void UpdateMotor()
{
SnapAlignCharacterWithCamera();
ProcessMotion();
}
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if(MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Mulitply MoveVector by MoveSpeed
MoveVector *= MovSpeed;
// Multiply MoveVector by DeltaTime
MoveVector *= Time.deltaTime;
// Move the Character in World Space
TP_Controller.CharacterCountroller.Move(MoveVector);
//TP_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
}
void SnapAlignCharacterWithCamera()
{
if(MoveVector.x != 0 || MoveVector.z != 0)
{
transform.rotation =Quaternion.Euler(transform.eulerAngles.x,
Camera.mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
}
TP_Controller:
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public static CharacterController CharacterCountroller;
public static TP_Controller Instance;
// Use this for initialization
void Awake()
{
CharacterCountroller = GetComponent("CharacterCountroller") as CharacterController;
Instance = this;
TP_Camera.UseExistingOrCreateNewMainCamera();
}
// Update is called once per frame
void Update()
{
if(Camera.mainCamera == null)
return;
GetLocomationInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomationInput()
{
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);
}
}
}
TP_Camera:
using UnityEngine;
using System.Collections;
public class TP_Camera : MonoBehaviour
{
public static TP_Camera Instance;
public Transform TargetLookAt;
// Use this for initialization
void Awake()
{
Instance = this;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public static void UseExistingOrCreateNewMainCamera()
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if(Camera.mainCamera != null)
{
tempCamera = Camera.mainCamera.gameObject;
}
else
{
tempCamera = new GameObject("MainCamera");
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;
}
}