Hello,
I am trying to fix a problem I have, when I have an enemy selected and it dies, which means that it is destroyed, then I cannot select the next enemy…
This is the error I get -
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
PlayerAttack.Attack () (at Assets/Resources/Script/PlayerAttack.cs:30)
PlayerAttack.Update () (at Assets/Resources/Script/PlayerAttack.cs:23)
&
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26)
Targetting.<SortTargetsByDistance>m__0 (UnityEngine.Transform t1, UnityEngine.Transform t2) (at Assets/Resources/Script/Targetting.cs:35)
System.Array.qsort[Transform] (UnityEngine.Transform[] array, Int32 low0, Int32 high0, System.Comparison`1 comparison) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:1772)
System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:1699)
Rethrow as InvalidOperationException: Comparison threw an exception.
System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:1702)
System.Collections.Generic.List`1[UnityEngine.Transform].Sort (System.Comparison`1 comparison) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections.Generic/List.cs:579)
Targetting.SortTargetsByDistance () (at Assets/Resources/Script/Targetting.cs:34)
Targetting.TargetEnemy () (at Assets/Resources/Script/Targetting.cs:42)
Targetting.Update () (at Assets/Resources/Script/Targetting.cs:90)
And here is the code -
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
void Start () {
targets = new List <Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
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)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget()
{
selectedTarget.renderer.material.color = Color.red;
selectedTarget.GetComponent<EnemyDeath>().Selected = true;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
selectedTarget.renderer.material.color = Color.white;
selectedTarget.GetComponent<EnemyDeath>().Selected = false;
selectedTarget = null;
}
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse1))
{
TargetEnemy();
}
}
}
I am new to programming, although I am learning, so any help with this would be great.
Thank you
Thanks for the reply :) I have followed the 1st suggestion and it works, when the enemy is destroyed it is deactivated and invisible, however now I am having difficulty finding a way to skip the disabled enemy from being targeted...
– ArczerThat works :) Thank you for all the help!
– ArczerAwesome, glad it worked. Feel free to mark the answer correct if ya like. :D
– whebert