Having trouble with this error. I am following a tutorial and disabled the navmesh agent after a certain trigger. It worked in the tutorial, but not for me. I already have applied the navmesh and baked it into the scene, so I’m not sure why I’m still getting this. The code is below. If it’s not something with the code I suspect it has something to do with the prefab.
Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float chaseRange = 5f;
[SerializeField] float turnSpeed = 3f;
EnemyHealth enemyHealth;
float distanceToTarget = Mathf.Infinity;
NavMeshAgent agent;
bool isProvoked;
// Start is called before the first frame update
void Start()
{
agent = GetComponent();
enemyHealth = GetComponent();
}
// Update is called once per frame
void Update()
{
if (enemyHealth.IsDead())
{
enabled = false;
agent.enabled = false;
}
distanceToTarget = Vector3.Distance(target.position, transform.position);
if (isProvoked)
{
EngageTarget();
}
else if (distanceToTarget <= chaseRange)
{
isProvoked = true;
}
}
public void OnDamageTaken()
{
isProvoked = true;
}
void EngageTarget()
{
FaceTarget();
if (distanceToTarget >= agent.stoppingDistance)
{
ChaseTarget();
}
if (distanceToTarget <= agent.stoppingDistance)
{
AttackTarget();
}
}
void ChaseTarget()
{
GetComponent().SetBool(“attack”, false);
GetComponent().SetTrigger(“move”);
agent.SetDestination(target.position);
}
void AttackTarget()
{
GetComponent().SetBool(“attack”, true);
print(“Attacking Target”);
}
private void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, chaseRange);
}
}
*Another Script Linking to this one below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
[SerializeField] float hitPoints = 100f;
bool isDead = false;
public bool IsDead()
{
return isDead;
}
public void TakeDamage(float damage)
{
BroadcastMessage(“OnDamageTaken”);
hitPoints -= damage;
if(hitPoints <= 0)
{
Die();
}
}
public void Die()
{
if (isDead) return;
isDead = true;
GetComponent().SetTrigger(“die”);
}
}