Unable to use navMeshAgent

i have the NavMeshAgent declared at the top of Mover.cs, and it is being set in start, but the navMeshAgent wont come through in the game and throws an error for object reference. I have it working without errors now but I am puzzled why navMeshAgent wont carry through. the intellisense doesn’t show any errors, and when typing it in I am able to pick out the other pieces such as .destination or .isStopped. I fixed it by reverting back to using GetComponent(). It works but I know its not right to keep repeating that.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

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

        NavMeshAgent navMeshAgent;

        private void start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
        }

        void Update()
        {
            UpdateAnimator();
        }

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

        public void Stop()
        {
            GetComponent<NavMeshAgent>().isStopped = true;
        }

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

as a side note, I deleted my mover.cs and pasted from github. that version is working fine so no idea what was wrong with mine.

lowercase ‘s’. Method calls are case sensitive. Your method start() was never called.

A good thing to think about though when debuging code is the flow of the program, if you arent sure about something, try follow it through with console logs or throwing exceptions and you will come up with the cause of the problem quite quick.

Caliber has the right answer. Start() is case sensitive (as are all of the Unity Callback Messages).

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

Privacy & Terms