- Home /
Sort By Distance, Call it multiple times.
using UnityEngine;
using System.Collections; using System.Collections.Generic;
public class TowerStone : MonoBehaviour {
public List targets; public GameObject projectile; private Transform target; private Transform myTransform; private float coolDown = 2; private float t; private delegate Transform myDelegate(Transform t1, Transform t2);
// Use this for initialization void Start () { myTransform = transform; float t = Time.time;
AddAllEnemies(); TargetEnemy(); }
// Update is called once per frame void Update () {
float c = Time.time - t; if (c > coolDown){ t = Time.time; Fire(); }
print(myTransform); //targets.RemoveAt(0); //print (c); }
void Fire(){ GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject; arrow.SendMessage("SetTarget", target);
}
public void AddAllEnemies(){ GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go){ AddTarget(enemy.transform);
} } private void SortTargetsByDistance(){
targets.Sort(delegate(Transform t1, Transform t2){ return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position)); });
}
public void AddTarget(Transform enemy){ targets.Add(enemy); }
private void TargetEnemy(){ if (target == null){ SortTargetsByDistance(); target = targets[0]; } } private void TargetDestroyed(){ // i would clear the list here if i knew how to AddAllEnemies(); TargetEnemy(); } }
Hello I want to sort this list by it's distance to the gameObject, the first time you call it, it works perfectly. When an enemy dies i call it again and try to erase the list without success by the way, i know i am supposed to delete it via listname.removeAll(); but i dont know what to put inside even though i've tried reading it's document.
I know the problem is on the SortTargetsByDistance method that was taken from a tutorial, the second time I call it this error appears:
issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
I guess i destroy it when the first item on the list is destroyed but i dont know how to create it again or not destroy it, i took it from a tutorial and i dont understant it.
Bottom line i want a way to sort this enemys by it's distance and be able to do it over and over again everytime one dies, and also want to know how to clear one list, vie removeAll if possible.
Thanks to everyone who takes time reading this and trying to help me.
click EDIT and format your code, use the "code" button (it has 1s and 0s on it)
Answer by freeaces · Feb 03, 2013 at 03:22 PM
If you destroy the dead enemy as an object, this would work fine as far as I know:
List<float> distances = new List<float>();
var go = GameObject.FindGameObjectsWithTag("Enemy");
for (int i = 0; i < go.Length; i++)
{
var distance = Vector3.Distance(transform.position, go[i].transform.position);
distances.Add(distance);
}
int index = distances.IndexOf(distances.Min());
TargetObj = go[index].transform;
Your answer
Follow this Question
Related Questions
Sorting list of Transforms 1 Answer
A node in a childnode? 1 Answer
Sort - OrderBy GameObjects by Distance 2 Answers
sort List alphabetically 2 Answers
List Sort not Returning Value 1 Answer