0

Im following Brackeys video on NavMesh movement, and i got this error right at the very end. I cant work out why, the only thing i have that is not initialized is Vector3.zero, but Brackeys does it in his video and it works fine.

The error i get is "NullReferenceException: Object reference not set to an instance of an object NavMeshPlayerController.Update() (at Assets/Scripts/NavMeshPlayerController.cs:40)

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    using UnityStandardAssets.Characters.ThirdPerson;

    public class NavMeshPlayerController : MonoBehaviour
    {
        public Camera cam;

        public NavMeshAgent agent;

        public ThirdPersonCharacter character;

        private void Start()
        {
            agent.updateRotation = false;
        }

        void Update()
        {
            //Left mouse has an index of 0
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = cam.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if(Physics.Raycast(ray, out hit))
                {
                    agent.SetDestination(hit.point);
                }
            }

            //If the character isnt at their destination, keep going usiong the desired velocity. If not, stop. 
            if (agent.remainingDistance > agent.stoppingDistance)
            {
                character.Move(agent.desiredVelocity, false, false);
            } else
            {
                character.Move(Vector3.zero, false, false);

            }


        }
    }
  • 2
    `character` is undefined. You probably didn't assign anything to it in the inspector. – Ruzihm Nov 20 '19 at 19:05
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/1092820) – Ruzihm Nov 20 '19 at 19:05
  • Character is just a reference given to a unity library is it not? I have public ThirdPersonCharacter character; at the top – MurrackCarn Nov 20 '19 at 19:10
  • 2
    It's a variable that contains a null reference. `public ThirdPersonCharacter character;` is a declaration of a reference variable, so its value defaults to null. – Ruzihm Nov 20 '19 at 19:11
  • Okay i think i understand, im very new to unity, i followed this video "https://www.youtube.com/watch?v=blPglabGueM" exactly and got this error. I have obviously make a silly mistake somewhere in the Unity UI because the code i have is exactly the same – MurrackCarn Nov 20 '19 at 19:17
  • Earlier I should have said "is not assigned anything" rather than it's "undefined". - it does have a definition and it defaults to `null`. – Ruzihm Nov 20 '19 at 19:17
  • Please watch the part at 13:17 very carefully. – Ruzihm Nov 20 '19 at 19:44
  • Thank you so much, i fixed that issue thanks to your help, but ive encountered another one. When i click play, my character falls through the world until his waist and he is unanimated. What could cause such a thing? – MurrackCarn Nov 20 '19 at 20:18
  • Happy to have helped, but you may want to make a different question for that, as it doesn't really match the title or body anymore. Also, you should include a screenshot of the character gameobject and all of its components shown in the inspector – Ruzihm Nov 20 '19 at 20:38

0 Answers0