Unity выдает ошибку
NullReferenceException: Object reference not set to an instance of an object ActorController.CharacterMove () (at Assets/Scripts/Actor/ActorController.cs:43) ActorController.Update () (at Assets/Scripts/Actor/ActorController.cs:32).
указывающую на строку
moveVector.x = mContr.Horizontal() * speedMove;
класса ActorController.
Не понимаю сути ошибки.
public class ActorController : MonoBehaviour {
//Скорость ходьбы
public float speedMove;
//Сила прыжка
public float jumpPower;
//Направление движения персонажа
private Vector3 moveVector;
private CharacterController ch_controller;
private Animator ch_animator;
private ActorMobileController mContr;
// Use this for initialization
void Start () {
ch_controller = GetComponent<CharacterController>();
ch_animator = GetComponent<Animator>();
mContr = GameObject.FindGameObjectWithTag("Joystick").GetComponent<ActorMobileController>();
}
// Update is called once per frame
void Update () {
GamingGravity();
CharacterMove();
}
//Метод перемещения персонажа
private void CharacterMove()
{
if (ch_controller.isGrounded)
{
moveVector = Vector3.zero;
moveVector.x = mContr.Horizontal() * speedMove;
moveVector.z = mContr.Vertical() * speedMove;
}
if (moveVector.x != 0 || moveVector.z != 0) ch_animator.SetBool("Move", true);
else ch_animator.SetBool("Move", false);
if (Vector3.Angle(Vector3.forward, moveVector) > 1f || Vector3.Angle(Vector3.forward, moveVector) == 0)
{
Vector3 direct = Vector3.RotateTowards(transform.forward, moveVector, speedMove, 0.0f);
transform.rotation = Quaternion.LookRotation(direct);
}
moveVector.y = gravityForce;
ch_controller.Move(moveVector * Time.deltaTime);
}
}
public class ActorMobileController : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
private Image joystickBG;
[SerializeField]
private Image joystick;
private Vector2 inputVector;
// Use this for initialization
void Start()
{
joystickBG = GetComponent<Image>();
joystick = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector2.zero;
joystick.rectTransform.anchoredPosition = Vector2.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBG.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / joystickBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / joystickBG.rectTransform.sizeDelta.x);
inputVector = new Vector2(pos.x * 2 - 1, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystick.rectTransform.anchoredPosition = new Vector2(inputVector.x * (joystickBG.rectTransform.sizeDelta.x / 2), inputVector.y * (joystickBG.rectTransform.sizeDelta.y / 2));
}
}
public float Horizontal()
{
if (inputVector.x != 0) return inputVector.x;
else return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (inputVector.y != 0) return inputVector.y;
else return Input.GetAxis("Vertical");
}
}