Hello I am having trouble with my scripting as it is giving me the error message
NullReferenceException: Object reference not set to an instance of an object
PlayerMotor.PerformMovement () (at Assets/PlayerMotor.cs:29)
PlayerMotor.FixedUpdate () (at Assets/PlayerMotor.cs:22)
I will post the code and i hope anyone can help.
PlayerMotor.cs…
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
private PlayerMotor motor;
void Start()
{
motor = GetComponent<PlayerMotor>();
}
void Update ()
{
//calculate movement velocity as 3d vector
float _xMov = Input.GetAxisRaw ("Horizontal");
float _zMov = Input.GetAxisRaw ("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
motor.Move(_velocity);
}
}
PlayerControler.cs…
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
private Rigidbody rb;
void start()
{
rb = GetComponent<Rigidbody>();
}
public void Move (Vector3 _velocity)
{
velocity = _velocity;
}
void FixedUpdate ()
{
PerformMovement();
}
//preform movement
void PerformMovement()
{
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
}