"NullReferenceException" error for "PlayerController"

,

I finished the lesson for “Namespaces To See Dependencies” for the “2019 RPG Core Combat Creator” course, but now when I click to move my character around, I get the following error message in the console:
“NullReferenceException: Object reference not set to an instance of an object
RPG.Control.PlayerController.MoveToCursor () (at Assets/Scripts/Control/PlayerController.cs:23)
RPG.Control.PlayerController.Update () (at Assets/Scripts/Control/PlayerController.cs:12)”
The console spits 5 or so of these errors whenever I click. It doesn’t affect gameplay, thankfully, but I don’t want it to be a problem further down the line, so I’m trying to fix it now.
Here’s my code for the PlayerController script:

using RPG.Movement;
using UnityEngine;

namespace RPG.Control
{
        public class PlayerController : MonoBehaviour
    {
        private void Update() 
        {
            if (Input.GetMouseButton(0))
            {
                MoveToCursor();
            }
        }

        private void MoveToCursor()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit Hit; 
            bool HasHit = Physics.Raycast(ray, out Hit);
            if (HasHit)
            {
                GetComponent<Mover>().MoveTo(Hit.point);          
            }
        }
    }
}

And here’s the code for the Mover script, in case it’s related:

using UnityEngine;
using UnityEngine.AI;

namespace RPG.Movement
{
        public class Mover : MonoBehaviour
    {
        // [SerializeField] Transform target;

        void Update()
        {
            UpdateAnimator();
        }

        public void MoveTo(Vector3 destination) 
        {
            GetComponent<NavMeshAgent>().destination = destination;
        }

        private void UpdateAnimator()
        {
            Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
            Vector3 LocalVelocity = transform.InverseTransformDirection(velocity);
            float speed = LocalVelocity.z; 
            GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
        }
    }
}

Please help.

It’s spitting out 5 or so of these errors every time you click? But the player is still moving where you want it to?
At a guess, do you have some dummy enemies with PlayerControllers attached to them?

The PlayerController relies on the Mover, meaning that a Mover component has to be attached to the same gameobject as the PlayerController. The error is caused when GetComponent() fails to return a mover.

Try adding this to the MoveToCursor as your first lines in the if(HasHit) block

if(GetComponent<Mover>()==null)
{
    Debug.Log($"{name} does not have a Mover component.");
    return;
}

This will help you quickly locate anything that has a PlayerController but does not have a Mover component.

Thanks for responding quickly!
I added those lines to my code, and sure enough I found the root of my problem: the Knight prefab I was using didn’t have a “Mover” component. Upon further investigation, I realized that it was coming from an extraneous “Animator” component attached to the childed prefab, which I removed. The game didn’t immediately crash on hitting “Play” and everything is working just fine, so unless you see a glaring mistake I didn’t know I made, I believe that solved the problem.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms