Hey guys I have a small error from a (probably) small error but I just can’t figure it out.
GameObject FindClosestEnemy()
{
// Find all game objects with tag Enemy
targets = GameObject.FindGameObjectsWithTag("Player");
float distance = Mathf.Infinity;
Vector3 position = transform.position;
// Iterate through them and find the closest one
foreach (GameObject target in targets) {
Vector3 diff = (target.transform.position - position);
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestTarget = target;
distance = curDistance;
}
}
return closestTarget;
}
Vector3 FindClosestPath()
{
//Find closest of 4 positions to nearest target
Vector3 trnsPos = transform.position;
float distance = Mathf.Infinity;
positions[0] = trnsPos + Vector3.forward;
positions[1] = trnsPos + Vector3.back;
positions[2] = trnsPos + Vector3.left;
positions[3] = trnsPos + Vector3.right;
foreach (Vector3 position in positions){
Vector3 diff = (closestTarget.transform.position - position);
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestPosition = position;
distance = curDistance;
}
}
return closestPosition;
}
I have two functions in one of them I’m calling the transform of that the object that it is attached to without error. However on the Vector 3 function I’m getting error when trying to do the same thing.
NullReferenceException: Object reference not set to an instance of an object Movement_Enemy.FindClosestPath () (at Assets/Scripts/Movement_Enemy.cs:85) Movement_Enemy.Update () (at Assets/Scripts/Movement_Enemy.cs:24)
This error is only happening while in playmode. If hope this question was written with enough clarity, thank you for you time.
My bad, line 85 is positions[0] = trnsPos + Vector3.forward;
Here’s the full script
using UnityEngine;
using System.Collections;
public class Movement_Enemy : MonoBehaviour {
public const float stepDuration = 0.5f;
private Coroutine playerMovement;
private Animation MovementAnimation;
private GameObject[] targets;
private GameObject closestTarget;
private Vector3[] positions;
private Vector3 closestPosition;
private void Start()
{
targets = GameObject.FindGameObjectsWithTag ("Player");
MovementAnimation = GetComponentInChildren<Animation> ();
}
private void Update()
{
if (Movement.playerMovement != null) {
FindClosestEnemy();
FindClosestPath();
print(closestTarget);
positions[0] = transform.position + Vector3.forward;
positions[1] = transform.position + Vector3.back;
positions[2] = transform.position + Vector3.left;
positions[3] = transform.position + Vector3.right;
print(closestPosition);
}
}
private IEnumerator Move(Vector3 direction)
{
print (direction);
if (direction == Vector3.left)
{MovementAnimation.Play("MovementLeft");}
if (direction == Vector3.right)
{MovementAnimation.Play("MovementRight");}
if (direction == Vector3.forward)
{MovementAnimation.Play("MovementForward");}
if (direction == Vector3.back)
{MovementAnimation.Play("MovementBackward");}
Vector3 startPosition = transform.position;
Vector3 destinationPosition = transform.position + direction;
float t = 0.0f;
while (t < 1.0f)
{
transform.position = Vector3.Lerp(startPosition, destinationPosition, t);
t += Time.deltaTime / stepDuration;
yield return new WaitForEndOfFrame();
}
transform.position = destinationPosition;
playerMovement = null;
}
GameObject FindClosestEnemy()
{
// Find all game objects with tag Enemy
targets = GameObject.FindGameObjectsWithTag("Player");
float distance = Mathf.Infinity;
Vector3 position = transform.position;
// Iterate through them and find the closest one
foreach (GameObject target in targets) {
Vector3 diff = (target.transform.position - position);
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestTarget = target;
distance = curDistance;
}
}
return closestTarget;
}
Vector3 FindClosestPath()
{
Vector3 trnsPos = transform.position;
float distance = Mathf.Infinity;
positions[0] = trnsPos + Vector3.forward;
positions[1] = trnsPos + Vector3.back;
positions[2] = trnsPos + Vector3.left;
positions[3] = trnsPos + Vector3.right;
foreach (Vector3 position in positions){
Vector3 diff = (closestTarget.transform.position - position);
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closestPosition = position;
distance = curDistance;
}
}
return closestPosition;
}
}