NullReferenceException: Object reference not set to an instance of an object
EnemyMovement.Update () (at Assets/scripts/Enemy/EnemyMovement.cs:27)
this is the script it is refering to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;
public class EnemyMovement : MonoBehaviour
{
public float lookRadius = 5f;
Transform target;
NavMeshAgent agent;
public float deathRadius = 5f;
// Start is called before the first frame update
void Start()
{
agent = GetComponent();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= deathRadius)
{
KillPlayer();
}
}
}
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 * 4f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, deathRadius);
}
public void KillPlayer()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
↧