x


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.

more ▼

asked Aug 11 '12 at 08:09 AM

Flague gravatar image

Flague
10 4 4

click EDIT and format your code, use the "code" button (it has 1s and 0s on it)

Aug 11 '12 at 09:58 AM Fattie

yes pls. Use the Code Button.

Jan 08 at 07:45 PM Karsnen
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
more ▼

answered Feb 03 at 03:22 PM

freeaces gravatar image

freeaces
1 2

(comments are locked)
10|3000 characters needed characters left
somelist.Clear();

Clears the list.

more ▼

answered Aug 11 '12 at 11:05 AM

zyzyx gravatar image

zyzyx
226 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1278
x356
x354
x57

asked: Aug 11 '12 at 08:09 AM

Seen: 365 times

Last Updated: Feb 03 at 03:22 PM