Odd Targetting Error on Runtime
so the error i am having has had me wondering what to do about it for quite a few months, and since i have moved on to other parts of my project
this script is attached to a homing missile that detects and sorts all current enemies by distance from the player and then selects the closest one as its target
the error thrown rarely on runtime is this:
NullReferenceException: Object reference not set to an instance of an object targetting.m__0 (UnityEngine.Transform t1, UnityEngine.Transform t2) (at Assets/Scripts/Combat Scripts/targetting.cs:58) System.Array.qsort[Transform] (UnityEngine.Transform[] array, Int32 low0, Int32 high0, >System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1773) System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1700) Rethrow as InvalidOperationException: Comparison threw an exception. System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1703) System.Collections.Generic.List`1[UnityEngine.Transform].Sort (System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:579) targetting.SortTargetsByDistance () (at Assets/Scripts/Combat Scripts/targetting.cs:57) targetting.TargetEnemy () (at Assets/Scripts/Combat Scripts/targetting.cs:74) targetting.Update () (at Assets/Scripts/Combat Scripts/targetting.cs:95)
and this error is spammed for about ten seconds(which is when the object this script is attached to is set to dissapear if it has not collided with anything) basically cutting fps in half
the only thing i have been able to determine is when this error occurs which is when it detects all the enemies and begins sorting but in the time between detecting and sorting, one of the enemies is killed and this error is thrown
this is the script and i'm not sure what to do about it coming up with a null reference while sorting
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class targetting : MonoBehaviour {
public Transform target;
private Vector3 v_diff;
private float atan2;
public float moveSpeed = 5;
public float trackSpeed;
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
public bool fire;
public bool canfire;
// Use this for initialization
void Start () {
if(Combat.energy > 15)
{
targets = new List<Transform>();
selectedTarget = null;
trackSpeed = 5;
myTransform = transform;
AddAllEnemies();
TargetEnemy();
}
else
{
gameObject.SetActive(false);
}
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); });
}
private void TargetEnemy()
{
if(selectedTarget == null && targets.Count == 1)
{
selectedTarget = targets[0];
}
if(selectedTarget == null && targets.Count > 1)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
}
// Update is called once per frame
void Update () {
if(selectedTarget != null)
{
fire = true;
v_diff = (selectedTarget.position - transform.position);
atan2 = Mathf.Atan2 (v_diff.y, v_diff.x);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg), trackSpeed * Time.deltaTime);
transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
}
if(selectedTarget == null)
{
targets.Clear();
targets = new List<Transform>();
AddAllEnemies();
TargetEnemy();
}
if(targets.Count == 0)
{
transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
}
}
}
i'm open to suggestions at this point
Your answer
![](https://koobas.hobune.stream/wayback/20220612133446im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GameObject instanciating only once: Runtime Error 1 Answer
Targetting Script Errors 0 Answers
Accessing Invalid Property 0 Answers
The 3D camera no longer exists 2 Answers