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);
}
}
}