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